中堅プログラマーの備忘録

忘れっぽくなってきたので備忘録として・・・

【Postfix】OP25B対策でGmailサーバーを経由してメールを送信する

1.概要

Outbound Port 25 Blocking(OP25B)が一般的になっており
内部ネットワークから外部ネットワークへのTCP25番ポートが遮断されています。
メールサーバーを構築している場合は25番ポートからは送信できない為
何かしらの対策が必要になります。


ここではSubmissionポートを使いGmailサーバーを経由して
メールを送信する方法の設定を行います。


OSはCentOS7になります。

2.図解

下図のとおりの構成で設定をしていきます。

f:id:tsu--kun:20191108185028p:plain

3.postfixをインストールする

まずはポストフィックスがインストールされているか確認します。

[root@localhost ~]# yum list installed | grep postfix
postfix.x86_64                        2:2.10.1-7.el7                   @anaconda

バージョン2.10.1がインストールされているようです。


※インストールされていない場合は

[root@localhost ~]# yum install postfix


バージョン確認のコマンドも送ってみます。

[root@localhost ~]# postconf | grep mail_version
mail_version = 2.10.1
milter_macro_v = $mail_name $mail_version

バージョン2.10.1で間違いないようです。


バージョンが古いようだったら下記コマンドでバージョンアップを行います。

[root@localhost ~]# yum -y update postfix

4.Postfixを設定する

【/etc/postfix/main.cf】の編集を行います。
編集内容は下記のとおりです。
※編集箇所のみを抜粋しています。

[root@localhost ~]# vi /etc/postfix/main.cf

myhostname = mail.hogehoge.com
mydomain = hogehoge.com
myorigin = $mydomain
inet_interfaces = localhost

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_security_level = encrypt
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = digest-md5 cram-md login plain
パラメータ 詳細
myhostname 本メールシステムのホスト名
mydomain 本メールシステムのドメイン名
myorigin ローカルから送られたメールに対して表示されるドメイン部分の設定
net_interfaces メールを受け取るインターフェース。今回は自分のとこからしか送らない(外部からのメールを中継しない)予定なのでlocalhostにする
relayhost リレー先のメールサーバーを設定します。[]で囲むとMX検索しない
smtp_sasl_auth_enable SASL 認証を有効にします
smtp_sasl_password_maps リレー先のサーバーのアドレスと、認証時に必要なユーザー名とパスワードを記述したファイルのパスを指定します
smtp_tls_security_level encryptを設定することでTLS暗号化通信を強制します。mayを設定すると中継先のメールサーバーが暗号化に対応している場合のみ暗号化します
smtp_sasl_tls_security_options anonymousで匿名ユーザーでのログインを許可しない
smtp_sasl_mechanism_filter 認証方法を指定します。最初に記述されている方法が優先されます。Gmailの場合は【LOGIN】になりますが、とりあえず一通り設定しています

smtp_sasl_password_mapsの作成

【main.cf】で指定したパス【/etc/postfix/sasl_passwd】を作成します。

[root@localhost ~]# vi /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 ******@gmail.com:パスワード


ファイルのパーミッションを変更します。

[root@localhost ~]# chmod 600 /etc/postfix/sasl_passwd


sasl_passwdをハッシュ化します。

[root@localhost ~]# postmap /etc/postfix/sasl_passwd

ファイルの状態を確認します。

[root@localhost ~]# ls -l /etc/postfix/sasl_passwd
-rw-------. 1 root root 54 Jan  1 11:51 /etc/postfix/sasl_passwd

5.SASLをインストールする

現在インストールされているSASLのパッケージを確認します。

[root@localhost ~]# yum list installed | grep sasl
cyrus-sasl.armv7hl              2.1.26-23.el7            @base
cyrus-sasl-devel.armv7hl        2.1.26-23.el7            @base
cyrus-sasl-lib.armv7hl          2.1.26-23.el7            @instCentOS/$releasever
cyrus-sasl-md5.armv7hl          2.1.26-23.el7            @base
cyrus-sasl-plain.armv7hl        2.1.26-23.el7            @base

この状態ならOKですが不足している場合はそれぞれインストールします。

[root@localhost ~]# yum install cyrus-sasl-devel

[root@localhost ~]# yum install cyrus-sasl-md5

[root@localhost ~]# yum install cyrus-sasl-plain

7.設定を反映させる

Postfixの再起動のみでOKです。

[root@localhost ~]# systemctl restart postfix


念のためPostfixの動作確認を行います。
緑の●の状態であれば動作しています。

[root@localhost ~]# systemctl status postfix
 postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 1970-01-02 07:27:10 JST; 3h 44min ago
 Main PID: 4444 (master)
   CGroup: /system.slice/postfix.service
           tq4444 /usr/libexec/postfix/master -w
           tq4446 qmgr -l -t unix -u
           tq4454 tlsmgr -l -t unix -u
           mq4728 pickup -l -t unix -u

8.Gmail側で必要な設定

【安全性の低いアプリの許可】を【有効】にする必要があります。
下記URLへアクセスし、メールを送信するGoogleアカウントでログインし設定します。

https://myaccount.google.com/lesssecureapps?pli=1

9.メールを送信する

メールを実際に送信してみます。
送信はmailコマンドから行いますので
ソフトがインストールされていなければ下記のとおりインストールを行います。

[root@localhost ~]# yum install -y mailx


送信!

[root@localhost ~]# echo "ここが本文" | mail -s "タイトルです" -r test@hogehoge.com *****@******.com

10.結果

結果は下図のとおりとなりました。
f:id:tsu--kun:20191108185044p:plain


ログを確認してみると下記のとおりとなりました。

[root@localhost ~]# cat /var/log/maillog | grep *****@******.com

Jan  2 11:25:34 localhost postfix/smtp[4915]: CC59F6048: to=<*****@******.com>, relay=smtp.gmail.com[64.233.189.109]:587, delay=3.9, delays=0.13/0.07/2/1.7, dsn=2.0.0, status=sent (250 2.0.0 OK  1573205874 f35sm4745789pje.32 - gsmtp)