LoginSignup
5
4

More than 3 years have passed since last update.

Amazon SESとPostfixの連携でハマったポイント

Last updated at Posted at 2019-12-25

こんにちは、MYLOPSのエンジニアの後河内です。
当社ではAmazon Linuxでサーバ環境構築した際にメールサービスにAmazon SESを利用することが多いです。

利用当初に、いくつかの設定でハマったことがあったのでその事を備忘録的に残して置こうと思います。

当初の設定

当初はpostfix+stunnel+Amazon SESという環境でメール送信の環境を作成しました。

その時の手順としては、以下になります。
*事前にAmazon SESにAmazon LinuxからアクセスできるようAWS アクセスキー(アクセスキー ID とシークレットアクセスキー)を取得しています。

設定手順

1.postfix, mailx, stunnel をインストール
$ sudo yum install mailx postfix stunnel

2.MTAをsendmailからpostfixに切り替える
$ sudo alternatives --config mta

2 プログラムがあり 'mta' を提供します。

 選択       コマンド
\-----------------------------------------------
*+  1           /usr/sbin/sendmail.sendmail
    2           /usr/sbin/sendmail.postfix

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:2

3.sendmailを停止する
$ sudo /etc/init.d/sendmail stop

4.stunnelの設定をする
$ sudo vi /etc/stunnel/stunnel.conf

/etc/stunnel/stunnel.conf
[smtp-tls-wrapper]
accept = 2525
client = yes
connect = email-smtp.us-west-2.amazonaws.com:465

5.認証ユーザの設定をする
$ sudo vi /etc/postfix/sasl_passwd

127.0.0.1:2525 <アクセスキー>:<シークレットキー>

6.認証ユーザーをPostfixで利用できるようにするデータベース化する
$ sudo postmap hash:/etc/postfix/sasl_passwd

7.Postfixをstunnel経由でSESにリレーする設定をする
*/etc/postfix/main.cf に下記を追加する

/etc/postfix/main.cf
#
# Amazon SES Setting
#
relayhost = 127.0.0.1:2525
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

8.Postfixのリスタートを行い、設定を反映する
$ sudo /etc/init.d/postfix restart

上記の設定をして、SESを利用できるようになりました。
設定当初は無事に送信できていたのですが、ある程度の期間何事もなく運用しておりました。しかしある時、急にメールが送信できなくなっていることに気づきました。

問題発生

当時、postfixのログには下記のようなログが残っておりました。

mail.log
postfix/smtp to=<hoge@hoge.hoge.jp>, relay=127.0.0.1[127.0.0.1]:2525, delay=29943, delays=29913/0.01/
30/0, dsn=4.4.2, status=deferred (lost connection with 127.0.0.1[127.0.0.1] while receiving the initial server greeting)

この時は、対処としては、stunnelとpostfixのサービスを再起動することでまたメール送信ができるようになりました。
原因についてサポートの方にも問い合わせをしたのですが、原因はわかりませんでした。

原因がわからなかったのですが、その際にサポートに紹介されたのが、stunnelを使わないpostfix+Amazon SES設定方法でした。

ちなみに、当時は上記の英語版のドキュメントは、stunnelを利用しない方法で記載があり、下記の日本語版のドキュメントはstunnelを利用する方法で書かれていたのですが、今確認すると日本語版もstunnelを利用しない方法になっていますね。

ちなみにサービスを再起動後も、ある程度の期間運用してると、そのうち送信できなくなるといったことがたびたび起きましたので、暫定対処としては、毎日1回、stunnelとpostfixのサービスを再起動するなどもしていたのですが、最終的にstunnelを利用しない方法に切り替えを行いました。

stunnelを利用しない方法

stunnelを利用しない方法に切り替えするために行ったものが以下の手順になります。

設定手順

1.cyrus-sasl-plain cyrus-sasl-md5をインストール
$ sudo yum install cyrus-sasl-plain cyrus-sasl-md5

2./etc/postfix/main.cfを変更する

/etc/postfix/main.cfの変更前
#
# Amazon SES Setting
#
relayhost = 127.0.0.1:2525
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

/etc/postfix/main.cfの変更後
#
# Amazon SES Setting
#
relayhost = email-smtp.us-west-2.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

3./etc/postfix/sasl_passwdを変更する

127.0.0.1:2525 <アクセスキー>:<シークレットキー>

email-smtp.us-west-2.amazonaws.com:587 <アクセスキー>:<シークレットキー>

4.認証ユーザー情報の更新する

$ sudo postmap hash:/etc/postfix/sasl_passwd

5.Postfixのリスタートをし設定を反映する

$ sudo /etc/init.d/postfix restart

上記の設定をすることで無事切り替えをすることができました。

stunnelを利用しない方法に変えてからは、特に送信できなくなったという問題は今の所、起きておりません。
いったい何が原因だったのでしょうかね。

5
4
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
5
4