FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour gmail
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

gmail

Posted: Sat Aug 10, 2024 08:47 AM
sendgmail.com
Code (fw): Select all Collapse
/*
**
**  sendGmail.prg - Compile with hbcurl.hbc
**  Send email via Gmail using oAuth v2
**  Need create a Google project and have access_token. See:
**  https://github.com/mod-harbour/mod_harbour.v2/tree/master/Samples/goauth
**
**  Developed by Fazio Diego(2024)
**
*/

#include "hbcurl.ch"

FUNCTION sendGmail( cGMAILuser, cGMAILaccesstoken, cSubject, cDestMail, cMsg, aAttach )

   LOCAL aHeader := {}, curl, ncurlErr, ncode, cJsonRes
   LOCAL cGUrl := "https://www.googleapis.com/gmail/v1/users/me/messages/send"
   LOCAL cBody := "", cFile

   hb_default( @aAttach, {} )

   AAdd( aHeader, "Authorization: Bearer " + AllTrim( cGMAILaccesstoken ) )
   AAdd( aHeader, "Content-Type: application/json" )

   cBody += "From: " + cGMAILuser + hb_eol()
   cBody += "To: " + cDestMail + hb_eol()
   cBody += "Subject: " + cSubject + hb_eol()
   cBody += "MIME-Version: 1.0" + hb_eol()
   cBody += 'Content-Type: multipart/mixed; boundary="boundary_string"' + hb_eol() + hb_eol()
   cBody += "--boundary_string" + hb_eol()
   cBody += 'Content-Type: text/plain; charset="UTF-8"' + hb_eol()
   cBody += "Content-Transfer-Encoding: 7bit" + hb_eol() + hb_eol()
   cBody += cMsg + hb_eol() + hb_eol()

   IF Len( aAttach ) > 0
      FOR EACH cFile in aAttach
         cBody += "--boundary_string" + hb_eol()
         cBody += 'Content-Type: text/plain; name="' + hb_FNameNameExt( cFile ) + '"' + hb_eol()
         cBody += 'Content-Disposition: attachment; filename="' + hb_FNameNameExt( cFile ) + '"' + hb_eol()
         cBody += "Content-Transfer-Encoding: base64" + hb_eol() + hb_eol()
         cBody += hb_base64Encode( hb_MemoRead( cFile ) ) + hb_eol()
      NEXT
   ENDIF

   cBody += "--boundary_string--" + hb_eol()

   curl_global_init()

   IF ! Empty( curl := curl_easy_init() )

      curl_easy_setopt( curl, HB_CURLOPT_HTTPHEADER, aHeader )
      curl_easy_setopt( curl, HB_CURLOPT_URL, cGUrl )
      curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
      curl_easy_setopt( curl, HB_CURLOPT_SSL_VERIFYHOST, .F. )
      curl_easy_setopt( curl, HB_CURLOPT_DOWNLOAD )
      curl_easy_setopt( curl, HB_CURLOPT_DL_BUFF_SETUP )
      curl_easy_setopt( curl, HB_CURLOPT_POSTFIELDS, hb_jsonEncode( { 'raw' => hb_base64Encode( cBody ) } ) )

      ncurlErr := curl_easy_perform ( curl )

      IF ncurlErr > 0
         ? "Curl Error: " + Str( ncurlErr )
      ENDIF

      ncode := curl_easy_getinfo( curl, HB_CURLINFO_RESPONSE_CODE )
      IF ncurlErr == 0
         cJsonRes := curl_easy_dl_buff_get( curl )
      ELSE
         cJsonRes := "Curl error"
      ENDIF

      curl_easy_cleanup( curl )

   ENDIF

   curl_global_cleanup()

RETURN cJsonRes
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 8523
Joined: Tue Dec 20, 2005 07:36 PM

Re: gmail

Posted: Sat Aug 10, 2024 02:13 PM

Good morning Master, have you tested this model? How Gmail works is very complicated. The best thing is to have your own provider for sending mass emails.

Buenos días Maestro, ¿ha probado este modelo? El funcionamiento de Gmail es muy complicado. Lo mejor es tener tu propio proveedor para el envío de correos electrónicos masivos.

Gracias, tks.

Regards, saludos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: gmail

Posted: Sat Aug 10, 2024 05:09 PM
Dear Joao,

I just copied and pasted the code :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com

Continue the discussion