【AWS】【Linux】EC2内のPostfixでAmazon SESにリレーしてメール送信する

■ 設定環境

 * EC2 / OS : CentOS7
 * Amazon SES設定済

■ 設定概要

[1] Postfixの導入
[2] PostfixAmazon SESにリレーしてメール送信するように設定
[3] 動作確認

■ 設定手順

[1] Postfixの導入

[1-1] Postfixがインストールされているか確認するために、以下を実行
~~~~
sudo yum list installed | grep postfix
~~~~

[1-2] インストールされていない場合は、以下を実行
~~~~
sudo yum install postfix
~~~~

[1-3] インストールしたPostfixを確認するために、以下を実行
~~~~
postconf  mail_version
~~~~

[1-4] Linux上で使用しているMTAをPostfixに設定するために、以下を実行
~~~~
sudo alternatives --config mta
# 「/usr/sbin/sendmail.postfix」の番号を選択
~~~~

[1-5] Postfixをスタートする
~~~~
systemctl start postfix

systemctl enable postfix
~~~~

[2] PostfixAmazon SESにリレーしてメール送信するように設定

[2-1] SMTP認証に必要なパッケージをインストールする
~~~~
sudo yum -y install cyrus-sasl-plain cyrus-sasl-md5
~~~~

[2-2] 「/etc/postfix/main.cf」を修正する

sudo vi /etc/postfix/main.cf
~~~~
# myhostname = 【ホスト名】
myhostname = smtp.hostname.jp

# relayhost = 【SMTP エンドポイント】:【ポート番号】
relayhost = email-smtp.us-east-1.amazonaws.com:587

smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt

# fix for CVE-2014-3556 (POODLE)
smtpd_tls_mandatory_protocols=!SSLv2,!SSLv3
smtp_tls_mandatory_protocols=!SSLv2,!SSLv3
smtpd_tls_protocols=!SSLv2,!SSLv3
smtp_tls_protocols=!SSLv2,!SSLv3
~~~~

[2-3] 「/etc/postfix/sasl_passwd」を作成
~~~~
# echo "【SMTP エンドポイント】:【ポート番号】 【ユーザ名】:【パスワード】" > /etc/postfix/sasl_passwd
sudo echo "email-smtp.us-east-1.amazonaws.com:587 username:password" > /etc/postfix/sasl_passwd

# ハッシュ化
sudo postmap hash:/etc/postfix/sasl_passwd

sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
~~~~

[2-4] Postfixを再起動
sudo systemctl restart postfix

[3] 動作確認

[3-1] sendmail コマンドで確認
~~~~
sendmail to_mail@gmail.com
From: from_mail@gmail.com
To: to_mail@gmail.com
Subject: This is a test mail.
[改行]
Hello world!
.
~~~~
 => 宛先にメールを受信
■ メールを受信できなかった場合
# 「/var/log/maillog」を参照し、原因を探る

sudo tail -f 15 /var/log/maillog