3
我想转发我反弹的电子邮件到一个PHP脚本来处理它们。我在用。用php脚本收集TO字段的电子邮件管道?
#!/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变量,但它不起作用。
任何帮助将是巨大的,
感谢,
编辑:其实我需要得到消息的正文中的“收件人”字段。 - 它弹回的电子邮件。我如何拆开消息的主体以获取特定信息?我应该使用该人的电子邮件创建一个特殊的标题,以便获取此信息更容易?
你需要知道它在消息体中的外观,然后用正则表达式来获取它。 – Alasdair 2012-02-05 09:55:26
有时更可靠的另一种选择是发送具有唯一返回路径的每条消息。通过这种方式,您可以根据消息被反弹到的电子邮件地址确切地知道哪些消息会返回给您。 – 2012-02-06 09:27:48