5.Postfixの設定

記事公開日:2015年02月04日

CentOS-6.6-x86_64-minimal.isoでインストールした場合、MTAはPostfixのみインストールされた状態です。
自宅で契約しているプロバイダでは、自宅サーバーから直接メールは送信できません。
Gmail経由で送るようにします。Googleアカウントとパスワードが必要です。

MTAの確認

MTA(Mail Transfer Agent)のソフトウェアであるPostfixが稼働しているか確認します。

# service postfix status
master (pid  1415) を実行中... <= 実行してなければservece postfix start
# chkconfig --list | grep postfix
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off <= 3:offならchkconfig postfix on

OSが使用するMTA(Mail Transfer Agent)がPostfixになっていることを確認します。

# alternatives --config mta

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

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

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

メールの送信

メールを送信してみます。

# echo test | mail xxxx@gmail.com
ログの確認 (日付は省略)
# tail -f /var/log/maillog
postfix/pickup[1703]: CCABA4375E: uid=0 from=
postfix/cleanup[1791]: CCABA4375E: message-id=<20150204072352.CCABA4375E@centos.localdomain>
postfix/qmgr[1422]: CCABA4375E: from=, size=427, nrcpt=1 (queue active)
postfix/smtp[1793]: connect to gmail-smtp-in.l.google.com[2404:6800:4008:c01::1b]:25: Network is unreachable
postfix/smtp[1793]: connect to gmail-smtp-in.l.google.com[173.194.72.27]:25: Connection timed out
postfix/smtp[1793]: connect to alt1.gmail-smtp-in.l.google.com[2607:f8b0:4003:c07::1b]:25: Network is unreachable
postfix/smtp[1793]: connect to alt1.gmail-smtp-in.l.google.com[64.233.168.27]:25: Connection timed out
postfix/smtp[1793]: connect to alt2.gmail-smtp-in.l.google.com[2607:f8b0:4001:c00::1b]:25: Network is unreachable
postfix/smtp[1793]: CCABA4375E: to=, relay=none, delay=61, delays=0.02/0/61/0, dsn=4.4.1, status=deferred (connect to alt2.gmail-smtp-in.l.google.com[2607:f8b0:4001:c00::1b]:25: Network is unreachable)

ログを確認すると上記のようにSMTPサーバーへ接続できないエラーがでています。
これは、現在接続しているプロバイダがOutbound Port 25 Blocking(アウトバウンドポート25ブロッキング、OP25B)をしている影響です。
試しにTelenetでGmailのメールサーバーへSMTP接続してみましょう。
Windowsのコマンドプロンプトで試しました。

C:>telnet alt1.gmail-smtp-in.l.google.com 25
接続中: alt1.gmail-smtp-in.l.google.com...ホストへ接続できませんでした。 ポート
番号 25: 接続に失敗しました

接続できませんね。

失敗したメールは、Postfixのキューに溜まっていますので削除しておきます。

キューの確認
# postqueue -p
キューをすべて削除
# postsuper -d ALL deferred

Postfixの設定

Posfixの設定ファイルの最終行に追記します。

# vi /etc/postfix/main.cf
# using gmail smtp server to relay mails
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_tls_CApath = /etc/pki/tls/certs/ca-bundle.crt

ログインユーザー情報ファイルを作成します。

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

ログインユーザー情報ファイルをハッシュ化し、権限変更してPostfix再起動します

# postmap /etc/postfix/sasl_passwd
/etc/postfix/sasl_passwd.db が生成されます
# chown root:root /etc/postfix/sasl_passwd
# chmod 600 /etc/postfix/sasl_passwd
# chown root:root /etc/postfix/sasl_passwd.db
# chmod 600 /etc/postfix/sasl_passwd.db
# service postfix restart

メールの送信

メールを送信してみます。

# echo test | mail xxxx@gmail.com
ログの確認 (日付は省略)
# tail -f /var/log/maillog
postfix/pickup[1974]: 363E343762: uid=0 from=
postfix/cleanup[1981]: 363E343762: message-id=<20150204083352.363E343762@centos.localdomain    >
postfix/qmgr[1975]: 363E343762: from=, size=427, nrcpt=1 (queue ac    tive)
postfix/smtp[1983]: connect to smtp.gmail.com[2404:6800:4008:c01::6d]:587: Network is unreach    able
postfix/smtp[1983]: 363E343762: to=, relay=smtp.gmail.com[173.194.72.10    9]:587, delay=3.6, delays=0.02/0.03/2.3/1.3, dsn=2.0.0, status=sent (250 2.0.0 OK 1423038835 b1sm1231475pdi.10 - gsmtp    )
postfix/qmgr[1975]: 363E343762: removed

ちゃんと送信できました。Gmailにログインし、正しく受信していることを確認できました。

root宛のメールをGmailに転送

cron処理時に送られるroot宛のメールをGmailに転送することもできます。

# vi /etc/aliases
最終行のrootのコメントを外して、アドレスを指定します。
root: xxxx@gmail.com 
保存して終了後、aliasesの設定を反映させます。
# newaliases

root宛にメール送信テスト

今度はroot宛にメールを送信してみます。

# echo test | mail root

/var/log/maillogと実際のGmailの受信トレイを確認し、正常に動作することを確認できました。

スポンサーリンク
←← 4.httpdとPHPとMySQLの導入
→→ 6.phpMyAdminの導入