超メモ帳(Web式)@復活

小説書いたり、絵を描いたり、プログラムやったりするブログ。統失プログラマ。


SPONSORED LINK

CentOSのPostfixでGmail経由でメールを出す+PHPで送信できない場合。



PostfixGmail経由でメールを送信するときに手間取った。Gmailで二段階認証を使っている場合はパスワードはアプリの固有パスワードを使えばいいみたいです。



アプリケーション固有のパスワードの取得 - Gmailの使い方



一応、手順をメモしておく。




iptablesのポートを開いておく。

# vim /etc/sysconfig/iptables



-A INPUT -m state --state NEW -m tcp -p tcp --dport 587 -j ACCEPTを追記。



# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.

filter

INPUT ACCEPT [0
0]
FORWARD ACCEPT [0
0]
OUTPUT ACCEPT [0
0]
  • A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  • A INPUT -p icmp -j ACCEPT
  • A INPUT -i lo -j ACCEPT
  • A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
  • A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  • A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT
  • A INPUT -m state --state NEW -m tcp -p tcp --dport 587 -j ACCEPT
  • A INPUT -m state --state NEW -m tcp -p tcp --dport 9001 -j ACCEPT
  • A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
  • A INPUT -j REJECT --reject-with icmp-host-prohibited
  • A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT



iptablesを再起動。



# service iptables restart

Postfixの設定



下記参考しました。



CentOSのPostfixからGmail経由でメールを出す : Tom's Lab



main.cfを記述。



myhostname = aaaa.bbbb.cccc.com
mydomain = bbbb.cccc.com
myorigin = $myhostname
inet_interfaces = localhost
mydestination = $myhostname, localhost.localdomain, localhost.$mydomain, localhost


# Gmail固有の設定
relayhost = [smtp.gmail.com]:587
# smtp.gmail.comはTLSによって暗号化する
smtp_use_tls = yes
# plain方式 (暗号化・符号化されていない) SMTP AUTH認証が必要になるので以下の記述を追加
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
# TLS証明書であるルート証明書を指定
# 指定しないとpostfixが/var/log/maillogにエラーを吐く
smtp_tls_CApath = /etc/pki/tls/certs/ca-bundle.crt



sase_passwdを記述。



[smtp.gmail.com]:587 user-name@gmail.com:mail-password
# chown root:root /etc/postfix/sasl_passwd
# chmod 600 /etc/postfix/sasl_passwd
# postmap /etc/postfix/sasl_passwd



postfixを再起動。



# service postfix restart

PHPから送信できない場合



ここまでやってPHPMailerでメール送信しようとした時に下記のようなメッセージが出る場合があります。



SMTP ERROR: Failed to connect to server: Permission denied (13)



下のコマンドで確認してoffの場合はSElinuxが悪さしています。



# getsebool httpd_can_sendmail



次のコマンドでonにしてやれば送れるようになります。



# setsebool -P httpd_can_sendmail 1



以上です。



プライバシーポリシー免責事項