vzの尺なblog

〜まだ 1 epoch 目〜

『postfix』 外部smtpサーバ(gmail)経由でメール送信する

動機

①運用しているWebサーバからメール送信したい ⇛ ②自前でsmtpサーバを立てる ⇛ ③gmail宛にメール送信すると、お前のsmtpサーバは信用ならないから中継しないと断れれる ⇛ ④困った ⇛ ⑤gmailアカウントでsmtp認証してそこからメール送信する。

※最近はセキュリティーが厳しく③のようにはじかれたりする。以下は関係してそうなwiki
Sender Policy Framework - Wikipedia

環境

普段DockerベースでWebサーバを運用しており、ベースイメージがDebian(Stretch)を使っているので、今回はこれがターゲットです。

Debian + postfix

Debian(Stretch) 9.6
Postfix 3.1.8-0+deb9u1

Dockerイメージ

使ったのは、php:7.1.24-apache-stretch です。元イメージは、debian:stretch-slim です。

gmailの設定

gmailアカウントでは、デフォルト設定だと OAth2認証のみ有効になっているようです。他の認証方式だとエラーになります。 gmailアカウントでログイン⇒ログインとセキュリティ⇒アカウントにアクセスできるアプリ⇒安全性の低いアプリの許可 を「有効」にする。 https://myaccount.google.com/security

f:id:vz-shark:20181210222049p:plain
安全性の低いアプリの許可を有効にする図

上記を許可しないと以下のようなmailログを吐いてエラーになりました。また使ったgmailアカウント宛に、「ログインをブロックしました」とメールが飛んできました。

Dec 10 12:58:45 e08929302e84 postfix/smtp[1743]: 1A08426107E: to=<your@gmail.com>, relay=smtp.gmail.com[108.177.97.108]:587, delay=2.4, delays=0.02/0.01/2.3/0, dsn=4.7.14, status=deferred (SASL authentication failed; server smtp.gmail.com[108.177.97.108] said: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=
~(中略)~
 Please log in via?534-5.7.14 your web browser and then try again.?534-5.7.14  Learn more at?534 5.7.14  https://support.google.com/mail/answer/78754 h19sm14266437pfn.114 - gsmtp)

sasl2

sasl2のインストール

apt install libsasl2-2 sasl2-bin libsasl2-modules

sasl2がインストールされていないと、mailログに

warning: SASL authentication failure: No worthy mechs found

と表示されてエラーになる。

postfix

postfixインストール

インストール途中で、設定を聞かれますが、後で設定ファイルを編集するので、ここでは「1の設定なし」を選択。

$ apt update -y
$ apt install -y postfix
Postfix Configuration
---------------------

Please select the mail server configuration type that best meets your needs.

 No configuration:
  Should be chosen to leave the current configuration unchanged.
 Internet site:
  Mail is sent and received directly using SMTP.
 Internet with smarthost:
  Mail is received directly using SMTP or by running a utility such
  as fetchmail. Outgoing mail is sent using a smarthost.
 Satellite system:
  All mail is sent to another machine, called a 'smarthost', for delivery.
 Local only:
  The only delivered mail is the mail for local users. There is no network.

  1. No configuration  2. Internet Site  3. Internet with smarthost  4. Satellite system  5. Local only
General type of mail configuration:  1 <リターン>

postfix設定ファイル

postfixの設定ファイル/etc/postfix/main.cfを編集。cp main.cf.prot main.cfで雛形をコピーして使います。いろいろ書いてあるけど、修正したのは以下の箇所

#自分の環境に合わせて書き換え
myhostname = host.mydomain
mydomain = mydomain
myorigin = $mydomain
 
#パス等の指定
sendmail_path = /usr/sbin/postfix
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
setgid_group = postdrop

#つかわないのでコメントアウト化
#html_directory =
#manpage_directory =
#sample_directory =
#readme_directory =

#relayとsaslの設定追記
# smtp server relay with sasl_auth
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_tls_security_level = encrypt
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CApath = /etc/pki/tls/certs/ca-bundle.crt

アカウント情報の設定

main.cfのsmtp_sasl_password_mapsに指定したファイルにアカウント情報を記述します。
経由するsmtpサーバ アカウント:パスワード
と記述します。

/etc/postfix/sasl_passwdを編集

$ touch /etc/postfix/sasl_passwd
$ chmod 600 /etc/postfix/sasl_passwd
$ vi /etc/postfix/sasl_passwd
smtp.gmail.com your_account@gmail.com:your_password

アカウント情報をハッシュ

$ postmap /etc/postfix/sasl_passwd

postfix再起動

$ /etc/init.d/postfix reload

テスト

mailコマンドとログ確認用のrsyslogの準備

このdockerイメージには、mailコマンドが入っていないので、mailutilsパッケージをインストールします。 また、syslogも入ってないので、インストールしてmailログが出れるよしておきます。

$ apt install -y mailutils rsyslog
$ /etc/init.d/rsyslog start

mail送信テスト

$ mail hogehoge@fugafuga.com
CC:
Subject: test mail sub
honnbunn1
honnbunn2
<CTL-D>

これでメールが届けばOK。
うまく送信できたら以下のようなログがでる・・・

to=<hoge@fuga.com>, relay=smtp.gmail.com[108.177.97.109]:587, delay=2.2, delays=0.01/0.01/1.1/1, dsn=2.0.0, status=sent (250 2.0.0 OK  - gsmtp)

うまくいかないときのログ強化

main.cfに以下を指定するとmail.logにデバック用の詳細な情報がでるのでこれで原因を確認。

debug_peer_list = smtp.gmail.com