Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SES template html part is multiple lines

I am using AWS SES to send emails by following the doc https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html. The HTML part in the sample template is too short, but I need a long HTML part with multiple lines. For example, it has several lines as the following:

{
    "Template": {
        "TemplateName": "Group_Invitation",
        "SubjectPart": "{{who}} has invited you to join team {{group_name}}",
        "TextPart": "",
        "HtmlPart": ["<!doctype html>
            <html>
            <head>
            <meta charset="utf-8">
            </head>
            <body>{{name}}</body>
            </html>"]
    }
}

I cannot upload this template. It will show errors

Error parsing parameter 'cli-input-json': Invalid JSON: Invalid control character at: line 6 column 32 (char 182)
JSON received: {
    "Template": {
        "TemplateName": "Group_Invitation",
        "SubjectPart": "{{who}} has invited you to join team {{group_name}}",
        "TextPart": "",
        "HtmlPart": ["<!doctype html>
            <html>
            <head>
            <meta charset="utf-8">
            </head>
            <body>{{name}}</body>
            </html>"]
    }
}

I am not sure how I can handle the htmlpart with multiple lines.

like image 590
Allen Tian Avatar asked May 07 '18 05:05

Allen Tian


People also ask

Does SES support HTML?

The Amazon SES API provides the SendEmail action, which lets you compose and send a formatted email. SendEmail requires a From: address, To: address, message subject, and message body—text, HTML, or both.

How do I create an email template in SES AWS?

To create a template to send personalized email messages, use the CreateTemplate operation. The template can be used by any account authorized to send messages in the AWS Region to which the template is added. Amazon SES doesn't validate your HTML, so be sure that HtmlPart is valid before sending an email.


2 Answers

The data you are sending should be preformatted to be a valid JSON file. To make sure it is valid you need to escape some special charaters:

  • Double quote " escaped as \"
  • Backslash \ escaped as \\
  • Newline

    escaped as \n

  • Carriage return escaped \r

There are some online tools that you can use to validate your JSON. One of them is jsonlint.com

Also take note that new lines in HTML are expressed as <br /> not as literal new line in a file.

Your JSON file should be formatted as following:

{
    "Template":{
        "TemplateName": "Group_Invitation",
        "SubjectPart": "{{who}} has invited you to join team {{group_name}}",
        "TextPart": "",
        "HtmlPart": "<!doctype html><html><head><meta charset=\"utf-8\"></head><body>{{name}}<br />some text on the other line</body></html>"
    }
}

Also you can use JSON Escape/ Unescape tool, and paste your HtmlPart, to quickly replace all the new lines and have it valid for sending via JSON.

Escaped HtmlPart

<!doctype html>\r\n            <html>\r\n            <head>\r\n            <meta charset=\"utf-8\">\r\n            <\/head>\r\n            <body>{{name}}<\/body>\r\n            <\/html>

You now can take this string, quote it and use it as HtmlPart. As you can see this tool escapes forward slashes as well but it is not required as stated in this answer

like image 196
Ernis Avatar answered Oct 08 '22 06:10

Ernis


If you are trying to get this done with AWS CLI v2 using create-email-template API, the template has changed and looks different now. You can obtain template skeleton with:

> aws sesv2 create-email-template --generate-cli-skeleton
{
    "TemplateName": "",
    "TemplateContent": {
        "Subject": "",
        "Text": "",
        "Html": ""
    }
}
like image 38
azec-pdx Avatar answered Oct 08 '22 06:10

azec-pdx