前提条件
- 送信元 From メールアドレスも送信先 To メールアドレスも SES で認証済みであること
メール送信できる IAM ユーザーを作成する
policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ses:SendRawEmail",
"Resource": "*"
}
]
}
SMTP ユーザ名、パスワードを作成する
SMTP ユーザー名は AWS アクセスキー ID と同じ
SMTP パスワードを IAM シークレットアクセスキーから生成する
以下のコードで変換する(Bash)
-
export AWS_SECRET_ACCESS_KEY={IAM シークレットアクセスキー} 後、bash実行
#!/usr/bin/env bash # These variables are required to calculate the SMTP password. VERSION='\x02' MESSAGE='SendRawEmail' # Check to see if OpenSSL is installed. If not, exit with errors. if ! [[ -x "$(command -v openssl)" ]]; then echo "Error: OpenSSL isn't installed." >&2 exit 1 # If OpenSSL is installed, check to see that the environment variable has a # length greater than 0. If not, exit with errors. elif [[ -z "${AWS_SECRET_ACCESS_KEY}" ]]; then echo "Error: Couldn't find environment variable AWS_SECRET_ACCESS_KEY." >&2 exit 1 fi # If we made it this far, all of the required elements exist. # Calculate the SMTP password. (echo -en $VERSION; echo -n $MESSAGE \ | openssl dgst -sha256 -hmac $AWS_SECRET_ACCESS_KEY -binary) \ | openssl enc -base64
生成されたSMTP パスワードを確認する
AWS アクセスキー ID と生成した SMTP パスワードを base64 エンコードする
-
AWS アクセスキー ID を base64 エンコード
-
echo -n "{AWS アクセスキー ID}" | base64
-
-
SMTP パスワードを base64 エンコード
-
echo -n "{SMTPパスワード}" | base64
-
-
AWS SMTP サーバへの TLS 接続をテスト
-
openssl s_client -crlf -quiet -starttls smtp -connect email-smtp.us-east-1.amazonaws.com:587
-
接続後、メール送信をテスト
AUTH LOGIN
{base64 エンコード後AWS アクセスキー ID}
{base64 エンコード後AWS シークレットアクセスキー}
- 235 Authentication successful. と表示後、以下を入力
MAIL FROM: {送信元メールアドレス}
RCPT TO: {送信先メールアドレス}
DATA
from:{送信元メールアドレス}
to:{送信先メールアドレス}
Subject: Test Subject
Test Mail
.
QUIT
- メールが受信できることを確認する