AWS SES template html part is multiple lines
Asked Answered
A

4

10

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.

Alienation answered 7/5, 2018 at 5:15 Comment(6)
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 visuallyLuann
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.Alienation
what tools are you using to send this json? javascript application, or some tool to test web requests?Luann
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.jsonAlienation
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?Parlor
But indeed it is; minify your HTML: I replace all the double quotes with \" and then use http://minifycode.com/html-minifier/ to, well, minifySadye
L
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

Luann answered 7/5, 2018 at 6:7 Comment(2)
Perfect solutionSkylar
I have the same challenge, I have no clue what is wrong with my JSON, do you have any idea? #68865910Roop
P
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.

Piton answered 27/10, 2022 at 11:45 Comment(0)
A
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": ""
    }
}
Antisepticize answered 23/2, 2021 at 6:39 Comment(0)
G
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.

Greer answered 17/7, 2023 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.