Dear friends,
I also had some issues with the mail-sending, but sending with the microsoft graph api and office365 is working very fine for me.
This is my code...it would be a great honor if some of it could be useful to you.
Kind regards
Ruth
#include "c:\harbour\contrib\hbcurl\hbcurl.ch"
#define HB_CURLOPT_POST 47
#define HB_CURLOPT_URL 2
#define HB_CURLOPT_DL_BUFF_SETUP 1008
#define HB_CURLOPT_POSTFIELDS 15
#define HB_CURLOPT_WRITEFUNCTION 11
FUNCTION Main()
LOCAL cToken, cResult
cToken := GetAccessToken()
IF !EMPTY(cToken)
cResult := SendEmail(cToken)
?"Email Send Result: ", cResult
ELSE
? "Failed to retrieve access token."
ENDIF
FUNCTION GetAccessToken()
LOCAL hCurl := CURL_easy_init()
LOCAL cPayload := ""
LOCAL cToken := ""
LOCAL cTest := ""
LOCAL nStartPos, nEndPos
LOCAL aHeaders := {"Content-Type: application/x-www-form-urlencoded"}
LOCAL cTenantID := hb_GetEnv("TENANT_ID")
LOCAL cClientId := hb_GetEnv("365client_id")
LOCAL cClientSecret := hb_GetEnv("365client_secret")
IF hCurl != NIL
CURL_easy_setopt(hCurl, HB_CURLOPT_URL, "https://login.microsoftonline.com/"+cTenantID+"/oauth2/v2.0/token")
CURL_easy_setopt(hCurl, HB_CURLOPT_HTTPHEADER, aHeaders)
CURL_easy_setopt(hCurl, HB_CURLOPT_POSTFIELDS, "grant_type=client_credentials&client_id="+cClientId+"&client_secret="+cClientSecret+"&scope=https://graph.microsoft.com/.default")
CURL_easy_setopt(hCurl, HB_CURLOPT_POST, .T.)
curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
CURL_easy_setopt(hCurl, HB_CURLOPT_WRITEFUNCTION, @cPayload) // Buffer the response
CURL_easy_perform(hCurl)
cPayload := curl_easy_dl_buff_get(hCurl) // Get the response from the buffer
nStartPos := AT('"access_token":"',cPayload)+LEN('"access_token":"')
nEndPos := hb_AT('"', cPayload, nStartPos)
cToken := SUBSTR(cPayload, nStartPos, nEndPos - nStartPos)
CURL_easy_cleanup(hCurl)
ELSE
? "Failed to initialize cURL session."
ENDIF
RETURN cToken
FUNCTION SendEmail(cToken)
LOCAL hCurl := CURL_easy_init()
LOCAL cEmailJson
LOCAL cResponse := ""
LOCAL aHeaders := {}
LOCAL cImgBase64 := ""
cImgBase64 := hb_base64Encode(hb_MemoRead("c:\www\htdocs\submarine_yellow\img\latest_news2.jpg"))
? "cImgBase64", cImgBase64
cEmailJson := '{"message": {"subject": "Test base64", ';
+ '"body": {"contentType": "html", ';
+ '"content": "<h1>test</h1><p>The new cafeteria is open...</p>';
+ '<img src=\"data:image/jpeg;base64,' + cImgBase64 + '\" > ';
+ '"}, ';
+ '"toRecipients": [{"emailAddress": {"address": "testrecip@****.info"}}]}, ';
+ '"saveToSentItems": "true"}'
IF hCurl != NIL
AADD(aHeaders, "Authorization: Bearer " + cToken)
AADD(aHeaders, "Content-Type: application/json")
CURL_easy_setopt(hCurl, HB_CURLOPT_URL, "https://graph.microsoft.com/v1.0/users/test@*****.at/sendMail")
CURL_easy_setopt(hCurl, HB_CURLOPT_HTTPHEADER, aHeaders)
CURL_easy_setopt(hCurl, HB_CURLOPT_POSTFIELDS, cEmailJson)
CURL_easy_setopt(hCurl, HB_CURLOPT_POST, .T.)
CURL_easy_setopt(hCurl, HB_CURLOPT_WRITEFUNCTION, @cResponse) // Buffer the response
CURL_easy_perform(hCurl)
cResponse := curl_easy_dl_buff_get(hCurl) // Get the response from the buffer
CURL_easy_cleanup(hCurl)
? "response", cResponse
ELSE
cResponse := "Failed to initialize cURL session."
ENDIF
RETURN cResponse