{SES}Amazon SES SMTP インターフェイスを使用して E メールを送信

 

https://docs.aws.amazon.com/ja_jp/ses/latest/dg/send-email-smtp.html
https://dev.classmethod.jp/articles/lets-try-ses-from-commandline/


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

 

-- 1.2 jqインストール
sudo yum -y install jq

 

-- 2. Identityの作成

aws ses verify-email-identity \
--email-address hoge@example.com

aws ses list-identities

aws ses get-identity-verification-attributes \
--identities hoge@example.com


-- 3. 動作確認

aws ses send-email \
--from hoge@example.com \
--to hoge@example.com \
--subject "subject01" \
--text "text01"


-- 4. 既存の AWS認証情報(AWS_SECRET_ACCESS_KEY)を変換して Amazon SES SMTP 認証情報(SMTPPassword)を取得する

vim smtp_credentials_generate.py

#!/usr/bin/env python3

import hmac
import hashlib
import base64
import argparse

SMTP_REGIONS = [
    'us-east-2',       # US East (Ohio)
    'us-east-1',       # US East (N. Virginia)
    'us-west-2',       # US West (Oregon)
    'ap-south-1',      # Asia Pacific (Mumbai)
    'ap-northeast-2',  # Asia Pacific (Seoul)
    'ap-southeast-1',  # Asia Pacific (Singapore)
    'ap-southeast-2',  # Asia Pacific (Sydney)
    'ap-northeast-1',  # Asia Pacific (Tokyo)
    'ca-central-1',    # Canada (Central)
    'eu-central-1',    # Europe (Frankfurt)
    'eu-west-1',       # Europe (Ireland)
    'eu-west-2',       # Europe (London)
    'sa-east-1',       # South America (Sao Paulo)
    'us-gov-west-1',   # AWS GovCloud (US)
]

# These values are required to calculate the signature. Do not change them.
DATE = "11111111"
SERVICE = "ses"
MESSAGE = "SendRawEmail"
TERMINAL = "aws4_request"
VERSION = 0x04


def sign(key, msg):
    return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()


def calculate_key(secret_access_key, region):
    if region not in SMTP_REGIONS:
        raise ValueError(f"The {region} Region doesn't have an SMTP endpoint.")

    signature = sign( ("AWS4" + secret_access_key).encode('utf-8'), DATE)
    signature = sign(signature, region)
    signature = sign(signature, SERVICE)
    signature = sign(signature, TERMINAL)
    signature = sign(signature, MESSAGE)
    signature_and_version = bytes([VERSION]) + signature
    smtp_password = base64.b64encode(signature_and_version)
    return smtp_password.decode('utf-8')


def main():
    parser = argparse.ArgumentParser(
        description='Convert a Secret Access Key for an IAM user to an SMTP password.')
    parser.add_argument(
        'secret', help='The Secret Access Key to convert.')
    parser.add_argument(
        'region',
        help='The AWS Region where the SMTP password will be used.',
        choices=SMTP_REGIONS)
    args = parser.parse_args()
    print(calculate_key(args.secret, args.region))


if __name__ == '__main__':
    main()

 

SMTPPassword=$(python3 smtp_credentials_generate.py BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB ap-northeast-1)

echo ${SMTPPassword}


SMTP ユーザー名(SMTPUsername)は AWS アクセスキー ID(AWS_ACCESS_KEY_ID) と同じです


-- 5. コマンドラインを使用して、Amazon SES SMTP インターフェイスへの接続をテストする


echo -n "AAAAAAAAAAAAAAAAAAAA" | openssl enc -base64

echo -n "${SMTPPassword}" | openssl enc -base64

 

vim input.txt

EHLO example.com
AUTH LOGIN
CCCCCCCCCCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
MAIL FROM: hoge@example.com
RCPT TO: hoge@example.com
DATA
From: Sender Name <hoge@example.com>
To: hoge@example.com
Subject: Amazon SES SMTP Test

This message was sent using the Amazon SES SMTP interface.
.
QUIT

cat input.txt

openssl s_client -crlf -quiet -starttls smtp -connect email-smtp.ap-northeast-1.amazonaws.com:587 < input.txt

 

 

-- 6. クリーンアップ

-- identityの削除

aws ses list-identities

aws ses delete-identity \
--identity hoge@example.com