postfixの設定
- /etc/postfix/aliasesに以下追記
noreply: "|/usr/bin/php /etc/postfix/noreply.php"
- 設定の反映
newaliases
処理
- /etc/postfix/noreply.php
<?php
// メールの受信データを取得
$emailData = file_get_contents("php://stdin");
// 送信者のメールアドレスを抽出
preg_match('/^From: .*<(.*)>/m', $emailData, $matches);
$sender = $matches[1] ?? '';
// 自動返信メールの内容を設定
$subject = "自動返信";
$message = "このメールは自動返信です。";
$subject = "=?ISO-2022-JP?B?" . base64_encode(mb_convert_encoding($subject, "ISO-2022-JP")) . "?=";
$message = mb_convert_encoding($message, "ISO-2022-JP");
// 返信ヘッダー
$headers = "From: noreply@example.com\r\n";
$headers.= "Content-Type: text/plain; charset=ISO-2022-JP\r\n";
$headers.= "Content-Transfer-Encoding: 7bit\r\n";
// 返信メールを送信
if (!empty($sender)) {
mail($sender, $subject, $message, $headers);
}
送信テスト
宛にメールを送信して自動返信メールがくることを確認。
ログの確認 (リアルタイム)
journalctl -f