Send emails from GMail

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('')
sMessage                        ANY
  !Next 2 options are the same as curl.AddHttpHeader('Authorization: Bearer '& pAccessToken)
  curl.SetOpt(CURLOPT_XOAUTH2_BEARER, pAccessToken)
  curl.AddHttpHeader('Accept: application/json')
  curl.AddHttpHeader('Content-Type: message/rfc822')

  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)
      MESSAGE(printf('Error: %i%|Status: %s%|Message: %s', errResp.ErrCode, errResp.ErrStatus, errResp.ErrMessage), 'SendMail', ICON:Exclamation)
  ELSIF res = -1
    MESSAGE('SendRequest failed: errcode '& res, 'SendMail', ICON:Asterisk)
    MESSAGE('SendRequest failed: '& curl.StrError(res), 'SendMail', ICON:Exclamation)

Note: anyone who has 2FA enabled on their google account cannot use “less secure apps”.

I had this situation for a customer and ended up having to write a small python app that my clarion app calls when sending email. It’s based on the example code google provides and implementation followed the process in their examples. Perhaps there’s a way to build that in-between code with your curl add on.

But you can generate application passwords for your app, and use them to bypass the 2FA.

True, but that doesnt work for gsuite users - and thanks for pointing that out, as the clarification is important to some.

Hi Mike,

Thank you for this example!
I tried it and it’s working great.

Best regards

There maybe a way around the 2FA checks.

So if you have a phone system which can receive txt msgs, like FOSS Freeswitch ( mod_sms - FreeSWITCH - Confluence) hooked up to an Analog Telephone Adapter (ATA ) which can handle SMS sent to landlines or a VoIP account.

Some landline telco’s in the UK dont allow sms to be sent, British Telecom do, TalkTalk dont, dont know about Virgin. Obviously other countries will be different but a quick test is to send a txt msg from your mobile to your landline and see if the telco has a bot which reads out the msg or directs you to a website to access the txt message.

If you use something like freeswitch, you can get voip phone numbers from some companies like (they do free numbers on a Pay As You Go (PAYG) basis) which are in the UK mobile phone number ranges that should make it possible to bypass the 2FA entirely in code. YMMV in other countries.

FYI: 2025 will see the entire UK phone system running entirely digital over IP, the old PSTN will be shut down.
The UK’s PSTN network will switch off in 2025 | BT Business

Some cabinets are already running digital today, probably those with fast Fibre and another FYI, the cabinet hardware will probably be Broadcom running firmware version 12.3.16 with VDSL2 17a (ITU G.993.2). I’ve seen reports that Chinese company Huawei also provide broadband exchange hardware, but I’ve not found any to date.