AWS SES 模板 html 部分是多行
AWS SES template html part is multiple lines
我正在使用 AWS SES 按照文档 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html 发送电子邮件。示例模板中的 HTML 部分太短,但我需要一个多行的长 HTML 部分。例如,它有如下几行:
{
"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>"]
}
}
我无法上传此模板。它会显示错误
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>"]
}
}
我不确定如何处理多行的 html 部分。
您发送的数据应预先格式化为有效的 JSON 文件。为确保它有效,您需要转义一些特殊字符:
- 双引号 " 转义为 \"
- 反斜杠 \ 转义为 \\
换行
转义为\n
回车return转义\r
您可以使用一些在线工具来验证您的 JSON。其中之一是 jsonlint.com
另请注意,HTML 中的新行表示为 <br />
而不是文件中的文字新行。
您的 JSON 文件格式应如下所示:
{
"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 发送.
转义 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 answer
中所述,它不是必需的
如果您尝试使用 create-email-template API 通过 AWS CLI v2 完成此操作,模板已更改,现在看起来有所不同。
您可以通过以下方式获取模板骨架:
> aws sesv2 create-email-template --generate-cli-skeleton
{
"TemplateName": "",
"TemplateContent": {
"Subject": "",
"Text": "",
"Html": ""
}
}
我正在使用 AWS SES 按照文档 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html 发送电子邮件。示例模板中的 HTML 部分太短,但我需要一个多行的长 HTML 部分。例如,它有如下几行:
{
"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>"]
}
}
我无法上传此模板。它会显示错误
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>"]
}
}
我不确定如何处理多行的 html 部分。
您发送的数据应预先格式化为有效的 JSON 文件。为确保它有效,您需要转义一些特殊字符:
- 双引号 " 转义为 \"
- 反斜杠 \ 转义为 \\
换行
转义为\n
回车return转义\r
您可以使用一些在线工具来验证您的 JSON。其中之一是 jsonlint.com
另请注意,HTML 中的新行表示为 <br />
而不是文件中的文字新行。
您的 JSON 文件格式应如下所示:
{
"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 发送.
转义 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 answer
中所述,它不是必需的如果您尝试使用 create-email-template API 通过 AWS CLI v2 完成此操作,模板已更改,现在看起来有所不同。 您可以通过以下方式获取模板骨架:
> aws sesv2 create-email-template --generate-cli-skeleton
{
"TemplateName": "",
"TemplateContent": {
"Subject": "",
"Text": "",
"Html": ""
}
}