LoginSignup
14
9

More than 5 years have passed since last update.

Postfixでメール送信する際に/etc/hostsを参照させる方法

Posted at

目的

vagrantで仮想マシン(CentOS-7.2)を立ち上げ、メール受信時に特定のPHPを実行する開発環境を構築したい。
簡単なメール送信環境を作るため、メール送信用にもう一台仮想マシンを起動。
vagrant間のローカルネットワークなので適当なドメイン作り、メール送信サーバーのhostsに記述したのだが、
/var/log/maillogに Host or domain name not found とエラーが出てうまく送信できない。

環境

# uname -r
3.10.0-327.13.1.el7.x86_64
# postconf | grep mail_version
mail_version = 2.10.1
milter_macro_v = $mail_name $mail_version
# alternatives --config mta

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

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

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

メール受信サーバー

  • IP:192.168.33.50
  • ホスト名:postfix

メール送信サーバー

  • IP:192.168.33.51
/etc/hosts
192.168.33.50 postfix

原因

Postfixはデフォルトではdns参照しかせず、サーバーの/etc/hostsは参照しない。

解決方法

Postfixの設定ファイルに下記を記述。

/etc/postfix/main.cf
smtp_host_lookup = native

Postfixを再起動

systemctl restart postfix

メール送信の際は先に/etc/hostsを参照し、書いていなければ通常通り/etc/resolv.confに指定されたネームサーバーへ問い合わせを行う。

メール受信時のPHPスクリプト実行

メール受信サーバー

Postfixの設定ファイルに下記を記述。

/etc/postfix/main.cf
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases

PHP実行のためにエイリアスを設定。最終行に追記。
vagrant: vagrant,としているのはvagrant宛のメールを残すため。

/etc/aliases
vagrant: vagrant, "| /usr/bin/php -f phpスクリプトファイルパス"

エイリアスを反映。

newaliases

メール送信サーバー

sendmailコマンドでメール送信。

# echo Test message! |sendmail vagrant@postfix

参考

Postfix に hosts を参照させる

14
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
9