postfix
June 10, 2008
Postfix - pass a mail to the php script
ある特定のユーザ、もしくはドメイン宛のメールをPHPに渡す場合のPostfixの設定。
まず、transportの設定をする。全ユーザの場合はドメイン名だけで良い。
まず、transportの設定をする。全ユーザの場合はドメイン名だけで良い。
$ vi /etc/postfix/transport user@example.com phpscript: # example.com phpscript:main.cfでこのファイルを指定する。
$ vi /etc/postfix/transport transport_maps = hash:/etc/postfix/transportpostmapコマンドでtransport.dbを更新する。
$ /usr/sbin/postmap /etc/postfix/transportmaster.cfで"phpscript"サービスの設定をする。
$ vi /etc/postfix/master.cf phpscript unix - n n - - pipe flags= user=nobody argv=/path/to/php /path/to/script.phppostfixを再起動する。
$ /etc/init.d/postfix restartphpではstdinからメールのソースを取得できる。
$ vi /path/to/script.php
<?php
$mail = file_get_contents("php://stdin");
April 17, 2008
mail function
phpでメール送信。毎回微妙に忘れるのでメモ。
Message-Idのドメインは引けるものであること。
また、パラメータのアドレスはメールが届くものであること。
Message-Idのドメインは引けるものであること。
また、パラメータのアドレスはメールが届くものであること。
function sendmail($from, $to, $subject, $body)
{
$headers = "MIME-Version: 1.0\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "Content-Type: text/plain; charset=ISO-2022-JP\r\n"
. "Message-Id: <" . md5(uniqid(microtime())) . ">@mail.example.com\r\n"
. "From: $from";
$parameter = "-f admin@example.com";
$subject = mb_encode_mimeheader($subject, "ISO-2022-JP");
$body = mb_convert_encoding($body, "ISO-2022-JP");
mail($to, $subject, $body, $headers, $parameter);
}
エンベロープのsenderアドレスを設定するために第5引数(additional_parameters)を指定すること。しかし、X-Warningヘッダが付加されてしまうので、それを回避するためにpostfixの設定が必要。これをしないとスパム扱いされたりする。vi /path/to/main.cf ... header_checks = regexp:/path/to/header_checks ...
vi /path/to/header_checks ... /^X-Authentication-Warning:/ IGNORE