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

Bash 如何使用AWS SES sendEmail API发送html邮件,bash,amazon-web-services,html-email,Bash,Amazon Web Services,Html Email,我正在尝试使用SendEmailAPI通过AWS SES发送HTML电子邮件 如果删除内容类型标题,效果会非常好 #!/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="secr

我正在尝试使用SendEmailAPI通过AWS SES发送HTML电子邮件

如果删除内容类型标题,效果会非常好

#!/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"
#/bin/bash
TO=”a@b.com"
FROM=”b@a.com"
SUBJECT=“测试对象”
MESSAGE=“测试消息
测试消息” 日期=“$(日期-R)” access_key=“” priv_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,算法=HmacSHA256,签名=$Signature” 端点=”https://email.us-west-2.amazonaws.com/" content\u type=“内容类型:text/html” mime_version=“mime版本:1.0” action=“action=sendmail” source=“source=$FROM” to=“Destination.toaddress.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“内容长度:50”--数据urlencode“$message”--数据urlencode“$to”--数据urlencode“$source”--数据urlencode“$action”--数据urlencode“$subject”$endpoint
但是当内容类型设置为text/html时,我得到了这个错误

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

无法确定要授权的服务/操作名称
请帮忙

如果删除内容类型标题,效果会非常好

是的,因为您在这里使用的
内容类型:text/html
标题是错误的

HTTP请求的
内容类型:
头与邮件正文无关——它是API请求的内容类型。正确的值是
application/x-www-form-urlencoded
——请注意,这就是使用
--数据urlencode
POST
正文进行编码的方式。。。这是正确的

因此,当您不手动设置它时,要么curl为您设置它,要么API为您节省一些时间并假设它是预期的编码,因为您没有另外指定。。。但是指定错误的编码,API会拒绝内容,因为它对接收系统没有意义

告诉SESAPI你正在发送一个HTML正文的方法是改变这个

message="Message.Body.Text.Data=$MESSAGE"
…为了这个

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


您还可以同时发送两个正文,纯文本和HTML,方法是同时包含这两个正文。这样,支持HTML多部分/可选功能的邮件阅读器将呈现HTML正文,其他更原始的邮件阅读器将呈现文本正文。

这不是对您问题的回答,但如果您使用AWS CLI:,您可以让自己的生活更轻松。)请参阅