LoginSignup
5
9

More than 5 years have passed since last update.

Amazon EC2 の Postfix から Gmail でメールを送信する

Last updated at Posted at 2016-09-11

Gmail の2段階認証を有効にする

  1. https://myaccount.google.com/security#signin から「2段階認証プロセス」のリンクを辿り、2段階認証を有効にする
  2. https://myaccount.google.com/security#signin から「アプリ パスワード」のリンクを辿り、アプリ用のパスワードを取得する

ソフトウェアのインストール

$ sudo yum install -y postfix mailx cyrus-sasl-plain cyrus-sasl-md5

Postfix の設定変更

$ sudo vi /etc/postfix/main.cf

以下の行を変更 / 追加する。

# 変更
#inet_protocols = all
inet_protocols = ipv4     # all -> ipv4 に変更、IPv6 を無効化

# 追加
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_password
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_use_tls = yes

パスワードの設定

ファイルを新規に作成する。

$ sudo vi /etc/postfix/sasl_password

以下の行を記述する。

[smtp.gmail.com]:587 ユーザ名@gmail.com:アプリ用パスワード
$ sudo chown root:root /etc/postfix/sasl_password
$ sudo chmod 600 /etc/postfix/sasl_password
$ sudo postmap /etc/postfix/sasl_password
$ sudo service postfix restart

動作確認

$ echo "test" | mail -s "test" 送信先メールアドレス
5
9
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
9