West Wind Internet and Client Tools
Using API rest, Can't send email with a PDF as an attachment
Gravatar is a globally recognized avatar based on your email address. Using API rest, Can't send email with a PDF as an attachment
  Denis Chassé
  All
  Jan 29, 2024 @ 05:35am

This is the structure that I have to follow for sending mail with SMTP2GO (email service) with their REST API

*!*	{
*!*	    "api_key": "api-40246460336B11E6AA53F23C91285F72",
*!*	    "to": ["Test Person <test@example.com>"],
*!*	    "sender": "Test Persons Friend <test2@example.com>",
*!*	    "subject": "Hello Test Person",
*!*	    "text_body": "You're my favorite test person ever",
*!*	    "html_body": "<h1>You're my favorite test person ever</h1>",
*!*	    "custom_headers": [
*!*	      {
*!*	        "header": "Reply-To",
*!*	        "value": "Actual Person <test3@example.com>"
*!*	      }
*!*	    ],
*!*	    "attachments": [
*!*	        {
*!*	            "filename": "test.pdf",
*!*	            "fileblob": "--base64-data--",
*!*	            "mimetype": "application/pdf"
*!*	        },
*!*	        {
*!*	            "filename": "test.txt",
*!*	            "fileblob": "--base64-data--",
*!*	            "mimetype": "text/plain"
*!*	        }
*!*	    ]
*!*	}

Everything works well sending emails as long as I don't specify an attachment

This is what I tried as for sending an attachment (PDF file).

lcFilePath = "Costs_Preprocess.pdf"	
lcBase64 = FILETOSTR(lcFilePath)	&& Read the contents of the file into a string
lcBase64 = STRCONV(lcBase64, 13)        && STRCONV with 13 parameter means Base64 encoding 
STRTOFILE(lcBase64, "Costs.pdf")


TEXT TO lcJson
{
    "api_key": "api-40246460336B11E6AA53F23C91285F72",
    "to": ["Moi <info@gestion-beaute.com>"],
    "sender": "Encore moi <testmlm@gestion-beaute.com>",
    "subject": "#1 Hello à moi",
    "text_body": "Je peux juste espérer.",
    "custom_headers": [
      {
        "header": "Reply-To",
        "value": "Denis Chassé <d.chasse@gestion-beaute.com>"
      }
    ],
    "attachments": [
        {
            "filename": "Costs.pdf",
            "fileblob": "--base64-data--",
            "mimetype": "application/pdf"
        }
    ]
}
ENDTEXT 

loProxy = CREATEOBJECT("wwJsonServiceClient")
lcJson = loProxy.CallService("https://api.smtp2go.com/v3/email/send",lcJson,"POST")
loProxy = null 

This is the error message I received.

{"request_id": "cbe76704-beb9-11ee-8cdc-f23c9216ce11", "data": {"error": "An error occurred processing the json data you sent with the request, please make sure the data conforms to the specification for this call (see the documentation here: https://apidoc.smtp2go.com/documentation/#/README) and try again. Don't forget to set Content-Type to 'application/json'.", "error_code": "E_ApiResponseCodes.NON_VALIDATING_IN_PAYLOAD", "field_validation_errors": {"fieldname": "fileblob", "message": "The field 'fileblob' was expecting a base64 encoded value but instead found '--base64-data--', Please correct your JSON payload and try again."}}}

I tried replacing

"fileblob": "--base64-data--",

with

"fileblob": "base64 encoded",

Didn't work. At this point I guess that you know that I'm out of ideas 😃

I looked for "base64 encoded" related to "Visual FoxPro" on the web. Nothing to really help me.

Suggestions?

Gravatar is a globally recognized avatar based on your email address. re: Using API rest, Can't send email with a PDF as an attachment
  Rick Strahl
  Denis Chassé
  Jan 29, 2024 @ 09:17am

Use a FoxPro blob for the value.

ADDPROPERTY(loObject, "fileblob", CAST(lcFileContent as Blob) )

It'll do the base64 conversion for you automatically.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Using API rest, Can't send email with a PDF as an attachment
  Denis Chassé
  Denis Chassé
  Jan 29, 2024 @ 11:29am

UPDATE

Weird things happening. I'll just clear that and get back when I'm done. In the meantime don't reply please

Regards

Excuse me but I don't see where I could put that line

ADDPROPERTY(loObject, "fileblob", CAST(lcFileContent as Blob) )

The way I understand this is that the content of file "Costs.PDF" should be base64 encoded. So I thought perhaps this would work


lcFilePath = "Costs_Preprocess.pdf"	
lcBase = FILETOSTR(lcFilePath)
lcBaseBlob = CAST( lcBase as BLOB)  
STRTOFILE(lcBaseBlob, "Costs.pdf")	&& Write the string to the file

But it doesn't. I received the same error (expexting base64 encoded).

Gravatar is a globally recognized avatar based on your email address. re: Using API rest, Can't send email with a PDF as an attachment
  Rick Strahl
  Denis Chassé
  Jan 30, 2024 @ 04:10pm

Maybe read the documentation?

Class wwJsonSerializer

+++ Rick ---

© 1996-2024