电子邮件管道到PHP脚本并转发到另一封电子邮件


email pipe to PHP script and forward to another email

我正在尝试编写一个脚本,该脚本将用于通过管道发送电子邮件,然后转发到另一个具有不同"发件人"字段的电子邮件地址

#!/usr/local/bin/php56 -q
<?php
$fd = fopen("php://stdin", "r");
$message = "";
while (!feof($fd)) {
$message .= fread($fd, 1024);
}
fclose($fd); 
//split the string into array of strings, each of the string represents a single line, received
$lines = explode("'n", $message);
// initialize variable which will assigned later on
$from = emailFrom@example.com;
$subject = "";
$headers = "";
$message = "";
$is_header= true;
//loop through each line
for ($i=0; $i < count($lines); $i++) {
if ($is_header) {
// hear information. instead of main message body, all other information are here.
$headers .= $lines[$i]."'n";
// Split out the subject portion
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
//Split out the sender information portion
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// content/main message body information
$message .= $lines[$i]."'n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}
mail( "emailTo@example.com", $subject,$message, $from );
?>

这个脚本让我们收到了发送失败的邮件"本地发送失败"。

我做错了什么?

您需要验证它是否正确解析了传入消息。在mail()命令后面添加此行,以验证脚本的输出:

echo "Subject:".$subject."'nFrom:".$from."'nMessage:".$message;

然后从命令行向脚本发送一封测试电子邮件,并观看屏幕。某些内容可能没有得到正确解析。