please could you be so kind as to help me out.
Here is a small piece of code that on my systems does not produce output for hb_base64encode()
- Code: Select all Expand view RUN
// C:\FWH\SAMPLES\RUTHBASE64.PRG
#include "FiveWin.ch"
#Include "Outlook.ch"
#Include "Mail.ch"
FUNCTION Main()
LOCAL cImgPath, cImgBase64, cImgMemo
cImgPath := "c:\fwh\samples\img\start_slide1.jpg"
cImgMemo := MEMOREAD(cImgPath)
cImgBase64 := hb_base64encode(MEMOREAD(cImgPath))
? "cImgPath", cImgPath
? "cImgMemo", cImgMemo
? "cImgBase64", cImgBase64
RETURN NIL
What is causing me a headache is that the hb_base64encode() produces output perfectly in my mod_harbour code. Please may I provide this too.
- Code: Select all Expand view RUN
#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
I would be so happy if anyone could run the small sample and tell me if they experience the same? Or what to do about it...
Very kind regards and thank you in advance,
Ruth