9

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.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Allen Tian
  • 93
  • 1
  • 1
  • 3
  • well the easiest fix would be to replace new lines with whitespaces, and send the request with a single line :) how are you sending the JSON, because the API requires a *HtmlPart* to be a single string, your new lines can be encoded (\r\n) but that would still be a single line string visually – Ernis May 07 '18 at 05:28
  • Ernis, thanks for your reply. But usually html email is more than one line. It is hard to put everything into one line and it will affect readability. – Allen Tian May 07 '18 at 05:37
  • what tools are you using to send this json? javascript application, or some tool to test web requests? – Ernis May 07 '18 at 05:39
  • Hi Ernis, I am using aws command to upload my template:aws --region=us-east-1 ses create-template --cli-input-json file://group_invitation.json – Allen Tian May 07 '18 at 05:40
  • This can't really be the only way to create a template ... right? I have three well-formed, 200+ line HTML files I'd like to use as templates. Am I really expected to cram each one of them into a JSON string value? – pdoherty926 Sep 21 '18 at 02:58
  • But indeed it is; minify your HTML: I replace all the double quotes with `\"` and then use [http://minifycode.com/html-minifier/](http://minifycode.com/html-minifier/) to, well, minify – hestellezg Apr 17 '19 at 22:23

4 Answers4

22

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

Ernis
  • 645
  • 11
  • 22
3

This worked for me for a large HTML file. First go to: Remove Line Breaks, select option Remove line breaks and paragraph breaks then paste its output in JSON Escape Tool, then use it in HtmlPart.

theGateway
  • 91
  • 1
  • 5
2

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": ""
    }
}
azec-pdx
  • 4,790
  • 6
  • 56
  • 87
1

An alternative solution would be to use a yaml file to upload the template contents. Here is an example I am using.

Here is the update command: aws sesv2 update-email-template --cli-input-yaml file://import_mailer.yaml --profile dev

Here is the yaml file contents.

TemplateContent:
  Subject:
    Charset: UTF-8
    Data: Example subject line
  Text:
    Charset: UTF-8
    Data: Example text content
  Html:
    Charset: UTF-8
    Data: |
    <html>
      <body>
        <h1>Example HTML content</h1>
      </body>
    </html>

Yaml allows for multi-line input for the HTML part.