php:Postfixで自動返信メール

メールを受信するサーバにメール受信用ユーザを登録する。

受信するメールアドレスは[ユーザ名]@ホスト名になります。

# adduser [ユーザ名]
# passwd [パスワード]

.forwardに転送設定を記述

/home/[ユーザ名]/.forward
register,”| /usr/bin/php /path/to/autoreply.php

.転送先はphpスクリプト。パイプでメール内容を渡している。

autoreply.php(返信処理の内容)はこんなふうになる。
[sourcecode language='php']
require_once(“Mail/mimeDecode.php”); //REARモジュール
//メールソースを標準入力から読み込み
$source = file_get_contents(“php://stdin”);
if(!$source) {
return false; // 読み込み失敗
}

//メール解析
$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$decoder = new Mail_mimeDecode($source);
$structure = $decoder->decode($params);

// 送信者のメールアドレスを取得する
$from_address = mb_convert_encoding(mb_decode_mimeheader($structure->headers['from'])
,mb_internal_encoding(), “auto”);
[/sourcecode]
このスクリプトを標準入力無しで起動すると

$source = file_get_contents(“php://stdin”);

のところで入力待ちになって処理が中断しますが、ヘッダさえもnullなメールを送ることは出来ないので多分気にしなくてもいいはず。

You can leave a response, or trackback from your own site.

Leave a Reply