我想将退回的电子邮件转发到 PHP 脚本来处理它们。我正在使用。
#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
// handle email
$lines = explode("\n", $email);
// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
// this is a header
$headers .= $lines[$i]."\n";
// look out for special headers
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
?>
工作完美!但是如何收集退回邮件中的“收件人”字段?我试过只添加一个 $to 变量,但它不起作用。
任何帮助都会很棒,
谢谢,
编辑:实际上我需要在邮件正文中获取“收件人”字段。 - 从中退回的电子邮件。如何拆分邮件正文以获取特定信息?我是否应该用此人的电子邮件创建一个特殊的标题,以便更容易地获取此信息?
最佳答案
如果您可以创建自定义 header ,那将是最简单的。否则,您需要针对特定模式匹配您的整个 body ;如果您的正文文本可能会有所不同,则可能很难确保您始终匹配正确的文本。
自定义 header 应以 X-
开头,因此可以执行以下操作:
if (preg_match("/^X-Originally-To: (.*)/", $lines[$i], $matches)) {
$originallyto = $matches[1];
}
但是对于 X- header ,它们是非标准的,因此最好选择一个名称,它是
- 通常专门用于同一目的,或
- 根本不可能被其他任何人使用
还有一件事你应该知道;消息中的行应始终以“\r\n”结尾,因此您可能希望拆分两个字符(而不仅仅是“\n”)以确保更一致的行为。
关于php - 使用 php 脚本的电子邮件管道来收集 TO 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9146674/