Sending emails from gmail using smtp protocol requires “Less secure apps” to be enabled in Google account. Otherwise you must use Google REST API.
Suppose you have a project in Google developer console which enables GMail library. Also suppose you already recieved an access token to this project.
Below is code snippet to send an email from gmail account using GMail service:
SendMail PROCEDURE(STRING pAccessToken, STRING pFrom, STRING pTo, STRING pSubject, STRING pBody)
curl TCurlClass
res CURLcode
dsResponse &IDynStr
endpoint STRING('https://www.googleapis.com/upload/gmail/v1/users/me/messages/send')
sMessage ANY
CODE
curl.Init()
!Next 2 options are the same as curl.AddHttpHeader('Authorization: Bearer '& pAccessToken)
curl.SetOpt(CURLOPT_HTTPAUTH, CURLAUTH_BEARER)
curl.SetOpt(CURLOPT_XOAUTH2_BEARER, pAccessToken)
curl.AddHttpHeader('Accept: application/json')
curl.AddHttpHeader('Content-Type: message/rfc822')
curl.SetHttpHeaders()
curl.SetSSLVerifyPeer(FALSE)
dsResponse &= NewDynStr()
!- build the message comnpliant with the RFC822 standard
sMessage = printf('From: %s%|To: %s%|Subject: %s%|%|%s', pFrom, pTo, pSubject, pBody)
res = curl.SendRequest(endpoint, sMessage, dsResponse)
IF res = CURLE_OK
ParseResponse(dsResponse.Str(), errResp)
IF errResp.ErrCode = 0
MESSAGE('Success!', 'SendMail', ICON:Asterisk)
ELSE
MESSAGE(printf('Error: %i%|Status: %s%|Message: %s', errResp.ErrCode, errResp.ErrStatus, errResp.ErrMessage), 'SendMail', ICON:Exclamation)
END
ELSIF res = -1
MESSAGE('SendRequest failed: errcode '& res, 'SendMail', ICON:Asterisk)
ELSE
MESSAGE('SendRequest failed: '& curl.StrError(res), 'SendMail', ICON:Exclamation)
END
DisposeDynStr(dsResponse)