如何使用 AWS SES sendEmail API 发送 html 邮件
How to send html mail using AWS SES sendEmail API
我正在尝试使用 sendEmail API 通过 AWS SES 发送电子邮件 HTML。
如果我删除内容类型 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"
但是当内容类型设置为 text/html 时,我收到了这个错误
<AccessDeniedException>
<Message>Unable to determine service/operation name to be authorized</Message>
</AccessDeniedException>
请帮忙。
It works perfect if I remove the content type header.
是的,因为你这里使用的Content-Type: text/html
header是错误的
HTTP 请求的 Content-Type:
header 与邮件 body 无关——它是 API 请求的内容类型。正确的值是 application/x-www-form-urlencoded
—— 请注意,这就是您 编码 POST
body 和 --data-urlencode
... 的方式是正确的。
因此,当您不手动设置它时,要么是 curl 为您设置,要么是 API 让您松懈并假设它是预期的编码,因为您没有另外指定...但是指定了错误的编码并且 API 拒绝了内容,因为它对接收系统没有意义。
告诉 SES API 您正在发送 HTML body 的方法是更改此...
message="Message.Body.Text.Data=$MESSAGE"
...到此...
message="Message.Body.Html.Data=$MESSAGE"
http://docs.aws.amazon.com/ses/latest/APIReference/API_Body.html
您也可以同时发送两个正文,纯文本和 HTML,方法是同时包含这两个正文。这样,HTML multipart/alternative-capable 邮件阅读器将呈现 HTML body,其他更原始的邮件阅读器将呈现文本 body.
我正在尝试使用 sendEmail API 通过 AWS SES 发送电子邮件 HTML。
如果我删除内容类型 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"
但是当内容类型设置为 text/html 时,我收到了这个错误
<AccessDeniedException>
<Message>Unable to determine service/operation name to be authorized</Message>
</AccessDeniedException>
请帮忙。
It works perfect if I remove the content type header.
是的,因为你这里使用的Content-Type: text/html
header是错误的
HTTP 请求的 Content-Type:
header 与邮件 body 无关——它是 API 请求的内容类型。正确的值是 application/x-www-form-urlencoded
—— 请注意,这就是您 编码 POST
body 和 --data-urlencode
... 的方式是正确的。
因此,当您不手动设置它时,要么是 curl 为您设置,要么是 API 让您松懈并假设它是预期的编码,因为您没有另外指定...但是指定了错误的编码并且 API 拒绝了内容,因为它对接收系统没有意义。
告诉 SES API 您正在发送 HTML body 的方法是更改此...
message="Message.Body.Text.Data=$MESSAGE"
...到此...
message="Message.Body.Html.Data=$MESSAGE"
http://docs.aws.amazon.com/ses/latest/APIReference/API_Body.html
您也可以同时发送两个正文,纯文本和 HTML,方法是同时包含这两个正文。这样,HTML multipart/alternative-capable 邮件阅读器将呈现 HTML body,其他更原始的邮件阅读器将呈现文本 body.