LoginSignup
0
0

More than 3 years have passed since last update.

openssl コマンドを使用して AWSのsmtp サーバからメールを送信する

Posted at

前提条件

  • 送信元 From メールアドレスも送信先 To メールアドレスも SES で認証済みであること

メール送信できる IAM ユーザーを作成する

policy
{
"Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ses:SendRawEmail",
            "Resource": "*"
        }
    ]
}

SMTP ユーザ名、パスワードを作成する

  1. SMTP ユーザー名は AWS アクセスキー ID と同じ

  2. SMTP パスワードを IAM シークレットアクセスキーから生成する

  3. 以下のコードで変換する(Bash)

  4. 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
    
  5. 生成されたSMTP パスワードを確認する

AWS アクセスキー ID と生成した SMTP パスワードを base64 エンコードする

  • AWS アクセスキー ID を base64 エンコード

    • echo -n "{AWS アクセスキー ID}" | base64
  • SMTP パスワードを base64 エンコード

    • echo -n "{SMTPパスワード}" | base64
  1. AWS SMTP サーバへの TLS 接続をテスト

    • openssl s_client -crlf -quiet -starttls smtp -connect email-smtp.us-east-1.amazonaws.com:587
  2. 接続後、メール送信をテスト

AUTH LOGIN
{base64 エンコード後AWS アクセスキー ID}
{base64 エンコード後AWS シークレットアクセスキー}
  • 235 Authentication successful. と表示後、以下を入力
MAIL FROM: {送信元メールアドレス}
RCPT TO: {送信先メールアドレス}
DATA
from:{送信元メールアドレス}
to:{送信先メールアドレス}
Subject: Test Subject
Test Mail
.
QUIT
  1. メールが受信できることを確認する

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0