hbCURL and Email ?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
serge_girard
Posts: 3420
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: hbCURL and Email ?

Post by serge_girard »

I do send MASS emailing with CURL:

Code: Select all

#include "hbcurl.ch"
#require "hbtip"
#require "hbssl"

PUBLIC cApiUrl     := "https://mandrillapp.com/api/1.0"   // API_URL
PUBLIC cAPIKey     := "somekey-d4590fdi90fdg80435rttt"    // API_KEY
PUBLIC cFromEmail  := "someome@website.com"               // MAN_FRMAIL
PUBLIC cFromName   := "Some One"                          // MAN_FRNAME
PUBLIC HTML_TEXT   := 'HTML'                              // MAN_HT_TXT
PUBLIC h, curlHandle, i, haTO, haTO_1, aTO, ahResp

// now begin sending
curlHandle := curl_easy_init()
IF EMPTY(curlHandle)
   Write_Trace('Error while init cURL lib '    )
 	MsgStop("Error while init cURL lib.")
   QUIT
	RETURN 
ENDIF

// loop or something
      cID := Send_Mandrill(cTo2, cSubject, cHTML_XX, '' )
      IF cID # "!ERROR!"
         ? 'error'
      ENDIF 
//endloop
curl_global_cleanup( curlHandle )
RETURN






FUNCTION Send_Mandrill(xTO, xSubject, xHtmlMess, xTextMess)
/**********************************************************/
LOCAL cURL, aTO, i,  h, cFromEmail, cSubject, cFromName
 ? 'Send_Mandrill : Prepare for send message'

cURL        := cApiUrl + "/messages/send.json"
 ? 'cURL', cURL


IF cMAN_HT_TXT == 'HTML'
   cTextMess   := ""    
   cHtmlMess   := xHtmlMess    
ELSE
   cTextMess   := xTextMess
   cHtmlMess   := ''
ENDIF


cSubject    := xSubject       
cFromEmail  := cMAN_FRMAIL 
cFromName   := cMAN_FRNAME

aTO         := {}
AADD(aTO, {xTO, xTO }) 
// add cc or bcc 
haTO        := {}
FOR i = 1 TO LEN( aTO )
	haTO_1 := { => }
	haTO_1 [ "email" ] := aTO [i] [1]
	haTO_1 [ "name" ]  := aTO [i] [2]
	AADD (haTO , haTO_1)
NEXT i



cHtmlMess := hb_StrDecodEscape( cHtmlMess )


h                 := { => }    
h [ "key" ]       := cAPIKey
h [ "message" ]   := {  "html"                        => hb_StrToUtf8( cHtmlMess, "FRWIN"), ;
				            "subject"                     => hb_StrToUtf8( cSubject , "FRWIN"), ;
                        "from_email"                  => cFromEmail, ;
                        "auto_text"                   => .F. , ;
                        "auto_html"                   => .T. , ;
                        "from_name"                   => cFromName, ;
                        "to"                          => haTO ,; 
                        "headers"                     => {"Reply-To" => cFromEmail} }

 


h [ "async" ]     := .F.
h [ "ip_pool"]    := "Main Pool"
 
cPOSTdata         := hb_jsonEncode( h , .T. )
cResp             := SendMandrill( cUrl, cPOSTdata, curlHandle )

IF cResp = "!ERROR!"
   cID      := "!ERROR!"
	Write_Trace(   'Report errors ', cResp )
ELSE
	hb_jsonDecode( cResp , @ahResp)
	Write_Trace(  'ahResp' , ahResp )
	Write_Trace(  'cResp ',  cResp )

   FOR i := 1 TO LEN( ahResp )
      cID := hb_HGet( ahResp [i] , "_id" ) 
   NEXT

ENDIF			
RETURN cID
Code is not 100% written by me! CURL-Json by Edward(EDK)

Hope this can help.....

Serge
There's nothing you can do that can't be done...
User avatar
AUGE_OHR
Posts: 2117
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: hbCURL and Email ?

Post by AUGE_OHR »

hi Serge,
serge_girard wrote: Wed Aug 24, 2022 6:58 amI do send MASS emailing with CURL:
thx for your Code.

i want to "test" it but don´t understand "how" it work

you point to JSON ... how does it look like :?:

Code: Select all

PUBLIC cAPIKey 
is it a UUID to use :?:
have fun
Jimmy
User avatar
serge_girard
Posts: 3420
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: hbCURL and Email ?

Post by serge_girard »

Hi Jimmy,

Most of the code was written by EDK (Edward) and JSON is beyond my knowledge.
First of all you need a Mandrill account+key. (see https://mailchimp.com/) Then it will give you access to the Mandrill API.

The aTO{} will contain (in my example) always 1 address.

I now see a function is missing in my example...Sorry, here it comes:

Code: Select all




Function SendMandrill (cUrl, cPOSTdata, curlHandle)
/****************************************************/
Local curlErr, cReturn

curl_easy_reset( curlHandle )

curl_easy_setopt( curlHandle, HB_CURLOPT_URL, cUrl )
curl_easy_setopt( curlHandle, HB_CURLOPT_FOLLOWLOCATION, .T. )
curl_easy_setopt( curlHandle, HB_CURLOPT_SSL_VERIFYPEER, .F. )

curl_easy_setopt( curlHandle, HB_CURLOPT_DOWNLOAD )
curl_easy_setopt( curlHandle, HB_CURLOPT_DL_BUFF_SETUP )
	
curl_easy_setopt( curlHandle, HB_CURLOPT_USERAGENT, "Mandrill-Curl/1.0" )
curl_easy_setopt( curlHandle, HB_CURLOPT_POST, .T. )
curl_easy_setopt( curlHandle, HB_CURLOPT_POSTFIELDSIZE, LEN( cPOSTdata ) )
curl_easy_setopt( curlHandle, HB_CURLOPT_POSTFIELDS, cPOSTdata )

curlErr := curl_easy_perform( curlHandle )		/* Do everything */

IF !EMPTY( curlErr )	/* Report any errors */
	cReturn := "!ERROR!" + CRLF + curl_easy_strerror(curlErr)
ELSE
	cReturn := curl_easy_dl_buff_get( curlHandle )
ENDIF

RETURN cReturn

Just signin to Mandrill (is MailChimp), create account etc, get an API-key and just try with this code (it works fine since almost 5 years now).
Remember it is not free although the website says 'Sign Up Free' (Sign Up is Free but using it is not...)

Serge
There's nothing you can do that can't be done...
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: hbCURL and Email ?

Post by mol »

Hi Serge. Can u write which libraries are needed to compile this sample?
User avatar
serge_girard
Posts: 3420
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: hbCURL and Email ?

Post by serge_girard »

Marek,

I use this in my HBP file:

Code: Select all

#hbmk2 options:
-lurlmon
 

In PRG:

Code: Select all

#include "hbcurl.ch"
#require "hbtip"
#require "hbssl"
And some DLL in attachement (CURL.RAR) +
2 rar files. Place them in folders with the names of the files.

Serge
Attachments
curl-7.53.1-win32-mingw.rar
(2.17 MiB) Downloaded 107 times
curl-7.40.0-devel-mingw32.rar
(3.74 MiB) Downloaded 94 times
CURL.rar
(1.78 MiB) Downloaded 99 times
There's nothing you can do that can't be done...
edk
Posts: 999
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: hbCURL and Email ?

Post by edk »

Here is an example I prepared for Serge once,
mandrill.7z
(2.85 MiB) Downloaded 131 times
you must have your API Mandrill Key.
It is a platform for mailing / conducting advertising and information campaigns.
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: hbCURL and Email ?

Post by mol »

Ricardo Arraes posted on ModHarbour function which sends messages with curl.
I've tested and it works!
Does anybody have contact to him?
Maybe he is able to help with communicating with imap server?


Maybe sb. will need, I'm placing here this function:

Code: Select all

FUNCTION sendSmtp(wto, wfrom, wpsw)

  LOCAL hCurl,uValue,nHandle,cTxt:=""

  curl_global_init()

  if ! empty( hCurl := curl_easy_init() )    

    cTxt:="From: Email Test <"+wfrom+">"+Chr(13)+Chr(10)+;
        "To: Somebody <"+Alltrim(wto)+">"+Chr(13)+Chr(10)+;
        "Subject: Sending email"+Chr(13)+Chr(10)+;
        "Date: Tue, 3 Feb 2021 20:40:16"+Chr(13)+Chr(10)+Chr(13)+Chr(10)+;
        "Hey, "+Chr(13)+Chr(10)+Chr(13)+Chr(10)+;
        "This is a test!"

    nHandle := Fcreate("C:\xampp\htdocs\example\tmp\test.txt")
    Fwrite(nHandle,cTxt )
    FClose(nHandle)

    curl_easy_setopt( hCurl, HB_CURLOPT_USE_SSL, HB_CURLUSESSL_TRY )	 
	  curl_easy_setopt( hCurl, HB_CURLOPT_UPLOAD )
    curl_easy_setopt(hCurl, HB_CURLOPT_USERNAME, wfrom)
    curl_easy_setopt(hCurl, HB_CURLOPT_PASSWORD, wpsw)
    curl_easy_setopt(hCurl, HB_CURLOPT_URL, "smtps://smtp.gmail.com:465")
    curl_easy_setopt( hCurl, HB_CURLOPT_PROTOCOLS, hb_bitOr( HB_CURLPROTO_SMTPS, HB_CURLPROTO_SMTP ) )	 
    curl_easy_setopt( hCurl, HB_CURLOPT_TIMEOUT_MS, 15000 )
    curl_easy_setopt(hCurl, HB_CURLOPT_FOLLOWLOCATION, 1)
    curl_easy_setopt(hCurl, HB_CURLOPT_SSL_VERIFYPEER, 0)
    curl_easy_setopt(hCurl, HB_CURLOPT_MAIL_FROM, wfrom)
    curl_easy_setopt(hCurl, HB_CURLOPT_MAIL_RCPT, {wto})
    curl_easy_setopt(hCurl, HB_CURLOPT_VERBOSE, 0)
    curl_easy_setopt(hCurl, HB_CURLOPT_UPLOAD, 1)    
    curl_easy_setopt( hCurl, HB_CURLOPT_UL_FILE_SETUP, "C:\xampp\htdocs\example\tmp\test.txt" )           


    IF (nret:=curl_easy_perform( hCurl )) == 0
      uValue := nret
    ELSE 
      uValue:=nret
    ENDIF
  ENDIF
  
  curl_global_cleanup()   

RETURN uValue
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: hbCURL and Email ?

Post by mol »

At now, I try to list IMAP folders.
There is a sample in curl library, but it is written in C, I'm very poor with it.
But I get error 77 described as 77 Problem with reading the SSL CA cert. The default or specified CA cert bundle could not be read/used to verify the server certificate.

There no info in sample to use any cert...
Even using windows curl to list folders, I'm getting them without any cert.
franco
Posts: 921
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: hbCURL and Email ?

Post by franco »

I do not understand what curl is.
I use gmail for sending emails. Is this not a good way. I have been using for many years.
Google has now set up a 2 step verification which allows send without setting security down. Google gives a special registered password,
just for email sending. The EXE is registered with google. I still use main password to going into account for setup.
I set forwarding to a different account for any return emails. This way the send account is only for sending out of my pos program.
I send invoices and Accounts Receivable Statements.
All The Best,
Franco
Canada
Post Reply