Skip to content

SES SendBulkTemplatedEmail: Cannot be Encoded in UTF-8?

0

I am trying to use the SES SendBulkTemplatedEmail service in Lambda to send emails, but I need to be able to encode them in UTF-8. Here is, in order, the request in Lambda using boto3, the template used, and the email received at the end of the process:

response = client.send_bulk_templated_email(
        Source=sender,
        ReturnPath=sender,
        Template=template_name,
        Destinations= [
            {
                'Destination': {
                    'BccAddresses': recipients_of_last_cluster_part
                },
                'ReplacementTemplateData':  replacement_template_data
                
            },
        ],
        DefaultTemplateData=replacement_template_data
    )
<!DOCTYPE html>
<html lang="fr">
   <head>
      <meta charset="UTF-8">
    </head>
    <body>
        <div> Testing characters </div>
        
        <div> é è ê à ù ë ï  </div>
    </body>
</html>

Enter image description here

I have not been able to find any help in the documentation, but I would that it is possible (in the template, or in the request) since you have the choice to choose a charset when using other functions like send_email.

1 Answer
0

Amazon SES supports sending email with content in any language. You're already using the correct charset (UTF-8) in your template, which should allow you to include special characters like é è ê à ù ë ï.

However, there might be an issue with how the content is being encoded or parsed. When providing content for ReplacementTemplateData and DefaultTemplateData, you should ensure that the content is JSON serialized and UTF-8 encoded.

Here's an example:

import boto3
import json

client = boto3.client('ses')

sender = 'sender@example.com'
template_name = 'template-name'
recipients_of_last_cluster_part = ['recipient@example.com']

replacement_template_data = {
    'key': 'value'
}

response = client.send_bulk_templated_email(
    Source=sender,
    ReturnPath=sender,
    Template=template_name,
    Destinations=[
        {
            'Destination': {
                'BccAddresses': recipients_of_last_cluster_part
            },
            'ReplacementTemplateData': json.dumps(replacement_template_data, ensure_ascii=False)
            
        },
    ],
    DefaultTemplateData=json.dumps(replacement_template_data, ensure_ascii=False)
)

In the code above, json.dumps(replacement_template_data, ensure_ascii=False) is used to ensure that non-ASCII characters are preserved.

If the issue still persists, the problem might lie in the creation of the template itself. It's not clear from your example how you are creating the template. If you're creating the template manually (i.e., not using the SES API), it might be an issue with the encoding of the file you're using. Make sure that the file itself is saved with UTF-8 encoding.

Also, keep in mind that the send_bulk_templated_email function sends emails asynchronously, meaning the function will return a success response before the emails have been sent. You can check for failures in the returned response object. Specifically, you would want to check the Failed field of the response object, which will contain an array of BulkEmailDestinationStatus objects that correspond to the destinations that you specified in the Destinations parameter. If any emails failed to send, the Status field of the corresponding BulkEmailDestinationStatus object will contain Failed, and the Error field will contain information about what went wrong.

answered 3 years ago

  • The suggestion with ensure_ascii=False does not solve my problem.

    I do not see how it could come from the template creation since I am simply using the AWS CLI to create and update it with the following command and JSON file:

    aws ses update-template --cli-input-json file://example-template.json

    { "Template": { "TemplateName": "example-template", "SubjectPart": "example subject", "HtmlPart": "<html><head><meta charset="UTF-8"</head><body> é è ê à ù ë ï </body>", "TextPart": "é è ê à ù ë ï" } }

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.