简体   繁体   English

如何使用 AWS SES sendEmail API 发送 html 邮件

[英]How to send html mail using AWS SES sendEmail API

I'm trying to send HTML email via AWS SES using sendEmail API.我正在尝试使用 sendEmail API 通过 AWS SES 发送 HTML 电子邮件。

It works perfect if I remove the content type header.如果我删除内容类型标题,它就完美无缺。

#!/bin/bash

TO="a@b.com"
FROM="b@a.com"
SUBJECT="test subject"
MESSAGE="<B>Test Message</B><br /> test message"

date="$(date -R)"
access_key="<aws key>"
priv_key="secret key>"
signature="$(echo -n "$date" | openssl dgst -sha256 -hmac "$priv_key" -binary | base64 -w 0)"
auth_header="X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=$access_key, Algorithm=HmacSHA256, Signature=$signature"
endpoint="https://email.us-west-2.amazonaws.com/"
content_type="Content-Type: text/html"
mime_version="MIME-Version: 1.0"
action="Action=SendEmail"
source="Source=$FROM"
to="Destination.ToAddresses.member.1=$TO"
subject="Message.Subject.Data=$SUBJECT"
message="Message.Body.Text.Data=$MESSAGE"

curl -v -X POST -H "$auth_header" -H "Date: $date" -H "$content_type" -H "$mime_version" -H "Content-Length: 50" --data-urlencode "$message" --data-urlencode "$to" --data-urlencode "$source" --data-urlencode "$action" --data-urlencode "$subject"  "$endpoint"

But with the content type set to text/html I'm getting this error但是将内容类型设置为 text/html 我收到此错误

<AccessDeniedException>
<Message>Unable to determine service/operation name to be authorized</Message> 
</AccessDeniedException>

Please help.请帮忙。

It works perfect if I remove the content type header.如果我删除内容类型标题,它就完美无缺。

Yes, because your use of the Content-Type: text/html header here is wrong.是的,因为您在这里使用Content-Type: text/html标题是错误的。

The Content-Type: header of the HTTP request has nothing to do with the mail body -- it's the content type of the API request. HTTP 请求的Content-Type:标头与邮件正文无关——它是 API 请求的内容类型。 The correct value is application/x-www-form-urlencoded -- notice that this is how you are encoding the POST body with --data-urlencode ... which is correct.正确的值是application/x-www-form-urlencoded -- 请注意,这是您使用--data-urlencodePOST正文进行编码的方式...这是正确的。

So, when you don't set it manually, either curl is setting it for you, or the API is cutting you some slack and assuming it's the expected encoding, since you didn't specify otherwise... but specify the wrong encoding and the API rejects the content, since it doesn't make sense to the receiving system.因此,当您不手动设置它时,要么 curl 为您设置它,要么 API 使您松懈并假设它是预期的编码,因为您没有另外指定......但指定了错误的编码和API 拒绝内容,因为它对接收系统没有意义。

The way to tell the SES API that you're sending an HTML body is to change this...告诉 SES API 您正在发送 HTML 正文的方法是更改​​此...

message="Message.Body.Text.Data=$MESSAGE"

...to this... ……到这……

message="Message.Body.Html.Data=$MESSAGE"

http://docs.aws.amazon.com/ses/latest/APIReference/API_Body.html http://docs.aws.amazon.com/ses/latest/APIReference/API_Body.html

You can also send both bodies together, plain text and HTML, by including both.您还可以通过包含两者一起发送两个正文,纯文本和 HTML。 That way, HTML multipart/alternative-capable mail readers will render the HTML body, other more primitive mail readers will render the text body.这样,支持 HTML 多部分/替代功能的邮件阅读器将呈现 HTML 正文,其他更原始的邮件阅读器将呈现文本正文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM