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

时间:2015-12-23 18:33:05

标签: php email cpanel

我正在尝试编写一个脚本,用于将电子邮件发送到它,然后转发到另一个电子邮件地址,其中包含不同的" From"字段

#!/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 );

?>

此脚本正在向我们发送带有投放失败消息的退回电子邮件&#34;本地投放失败&#34;。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

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

然后从命令行向脚本传递测试电子邮件并观看屏幕。可能没有正确解析的东西。