2016-04-20 141 views
1

我想通過使用sendEmail API的AWS SES發送HTML電子郵件。如何使用AWS SES發送HTML郵件sendEmail API

如果我刪除內容類型標題,它會很完美。

#!/bin/bash 

TO="[email protected]" 
FROM="[email protected]" 
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" 

但隨着設置爲文本內容類型/ HTML我得到這個錯誤

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

請幫助。

+0

不是您的問題的答案,但如果您使用AWS CLI,則可以讓自己更輕鬆:)參見http://docs.aws.amazon.com/cli/latest/reference/ses/send-raw -email.html – mickzer

回答

0

如果我刪除內容類型標題,它會很完美。

是的,因爲您在這裏使用的Content-Type: text/html標題是錯誤的。

HTTP請求的Content-Type:標頭與郵件正文無關 - 它是API請求的內容類型。正確的值是application/x-www-form-urlencoded - 請注意,這是你如何編碼POST身體與--data-urlencode ......這是正確的。

因此,如果您沒有手動設置它,curl會爲您設置,或者API正在削弱您一些鬆弛,並且假設它是預期的編碼,因爲您沒有另行指定......但指定錯誤的編碼和API拒絕內容,因爲它對接收系統沒有意義。

的方式告訴您發送的HTML身體是改變這個SES API ...

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的郵件閱讀器將呈現HTML正文,其他更原始的郵件閱讀器將呈現文本正文。

+0

謝謝邁克爾,它工作。 – kanadenipun