gmail

gmail

Postby Antonio Linares » Sat Aug 10, 2024 8:47 am

sendgmail.com
Code: Select all  Expand view  RUN
/*
**
**  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
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: gmail

Postby karinha » Sat Aug 10, 2024 2: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
User avatar
karinha
 
Posts: 7832
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: gmail

Postby Antonio Linares » Sat Aug 10, 2024 5:09 pm

Dear Joao,

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 39 guests