【PHP】mb_send_mailでメールが送れない → postfixが入っていなかった

この記事について

環境PHP + Linux(postfix)
目的mb_send_mail が false を返してメールが送れないのを解消
結果postfix をインストールして起動したら送れた

つまずきポイント要約

  • sendmail_path が設定されていても postfix 本体がなければ送れない
  • postfix 起動時の「no local interface found for ::1」は、ipv6が無効なのに /etc/hosts に ::1 の行があるのが原因
  • mb_send_mail は送れたが mail 関数は bounced で送れなかった

事象

if(mb_send_mail($to, $subject, $message, $headers)) {
     echo "メール送信成功です";
} else {
     echo "メール送信失敗です";
}

false が返ってメールが送れない。

原因を調査

phpinfoで調べる

sendmail_path /usr/sbin/sendmail -t -i

オプションについて

「-t」は、メールの宛先をヘッダから自動的に設定するオプション。「-i」は、本文中のドットから始まる行を終了文字列として扱わないようにするオプション。

postfixがインストールされているかの確認

第23章 postfix | SELinux ユーザーおよび管理者のガイド | Red Hat Enterprise Linux | 7 | Red Hat Documentation
第23章 postfix | SELinux ユーザーおよび管理者のガイド | Red Hat Enterprise Linux | 7 | Red Hat Documentation
$ rpm -q postfix
package postfix is not installed

対処:postfix をインストール

ないのでインストール。

yum install postfix
systemctl enable postfix ← Postfix自動起動設定
Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /usr/lib/systemd/system/postfix.service.

postfix が起動しない

起動させようとしたらエラーが出た。

systemctl start postfix ← Postfix起動
Job for postfix.service failed because the control process exited with error code.
See "systemctl status postfix.service" and "journalctl -xeu postfix.service" for details.

エラー内容にあるようにコマンドを実行。

#二つとも実行した
systemctl status postfix.service
journalctl -xeu postfix.service

どちらのコマンドを実行しても共通のエラーが赤文字で出ていた。

fatal: parameter inet_interfaces: no local interface found for ::1
https://www.softel.co.jp/blogs/tech/archives/5651

ipv6が無効なのに、/etc/hosts に以下の設定があると発生するらしい。

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

::1 の行をコメントアウトするといい。

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

再び起動を試みる。

systemctl start postfix ← Postfix起動

起動できたので、mb_send_mail の記述があるファイルを再読み込みすると、true が返って送れた。

mb_send_mail は 送れるが mail 関数だとエラーが出て送れなかった。

cat /var/log/maillog

status=bounced
タイトルとURLをコピーしました