0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自動返信メール (postfix)

Posted at

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 
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?