AWS SES 模板 html 部分是多行 [英] AWS SES template html part is multiple lines

查看:20
本文介绍了AWS SES 模板 html 部分是多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AWS SES 按照文档https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html.示例模板中的 HTML 部分太短,但我需要一个多行的长 HTML 部分.例如,它有如下几行:

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>"]
    }
}

我不确定如何处理多行的 htmlpart.

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

推荐答案

您发送的数据应预先格式化为有效的 JSON 文件.为了确保它有效,您需要转义一些特殊字符:

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

转义为 \n

回车转义 \r

您可以使用一些在线工具来验证您的 JSON.其中之一是 jsonlint.com

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

另请注意,HTML 中的新行表示为 <br/> 而不是文件中的文字换行.

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

您的 JSON 文件应采用以下格式:

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>"
    }
}

您也可以使用 JSON Escape/Unescape 工具,然后粘贴您的HtmlPart,快速替换所有新行并使其有效用于通过 JSON 发送.

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.

转义 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>

您现在可以使用此字符串,引用它并将其用作 HtmlPart.如您所见,此工具也可以转义正斜杠,但如 this 答案

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

这篇关于AWS SES 模板 html 部分是多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆