All*_*ian 4 json amazon-web-services amazon-ses
我正在使用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>"]
}
}
Run Code Online (Sandbox Code Playgroud)
我无法上传此模板。它将显示错误
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>"]
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何处理多行的htmlpart。
您发送的数据应预先格式化为有效的JSON文件。为了确保它有效,您需要转义一些特殊字符:
新队
转义为\ n
回车符逃脱\ 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>"
}
}
Run Code Online (Sandbox Code Playgroud)
另外,您可以使用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>
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用此字符串,将其引用并用作HtmlPart。如您所见,此工具也可以转义正斜杠,但并非如本答案所述那样
| 归档时间: |
|
| 查看次数: |
2787 次 |
| 最近记录: |