2016-07-05 38 views
0

我寫了這個劇本通過SES發送郵件不能正常工作

#!/bin/bash 

TO="[email protected]" 
FROM="[email protected]" 
SUBJECT="test" 
MESSAGE="test message" 

date="$(date -R)" 
echo $date 
priv_key="my_access_key" 
access_key="my_access_key_id" 
signature="$(echo -n "$date" | openssl dgst -sha256 -hmac "$priv_key" -binary | base64 -w 0)" 
echo $signature 
auth_header="X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=$access_key, Algorithm=HmacSHA256, Signature=$signature ,Date=$date" 
endpoint="https://email.us-east-1.amazonaws.com/" 

action="Action=SendRawEmail" 
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 "Date: $date" -H "$auth_header" --data-urlencode "$message" --data-urlencode "$to" --data-urlencode "$source" --data-urlencode "$action" --data-urlencode "$subject" "$endpoint" 

但我收到此錯誤:

<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/"> 
    <Error> 
    <Type>Sender</Type> 
    <Code>InvalidClientTokenId</Code> 
    <Message>The security token included in the request is invalid</Message> 
    </Error> 
</ErrorResponse> 

我無法理清的問題,即其安全令牌它正在談論。這是關於我的憑據。我創建了一個具有發送原始郵件角色的IAM用戶。

回答

0

首先,您需要轉到IAM控制檯並創建角色或用戶,並向用戶添加策略以使用SES發送電子郵件。

您不應該將憑據寫入代碼。有更安全和更正確的方式來做到這一點。例如,如果您正在使用EC2,則應該將角色添加到您的EC2實例。另一種解決方案是使用環境變量(http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)。

當用戶擁有此策略時,還需要驗證AWS賬戶中SES儀表板中的發件人電子郵件。您可以驗證收件人電子郵件,也可以創建向未註冊的收件人發送電子郵件的請求。

但首先嚐試添加IAM權限並嘗試使其工作。