2015-12-23 59 views
3

我想写这将被用来获取电子邮件管道输送到它,然后转发到一个不同的另一个电子邮件地址“发件人”字段的脚本电子邮件管PHP脚本,并转发到另一个电子邮件

#!/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 = [email protected]; 
$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("[email protected]", $subject,$message, $from); 

?> 

这个脚本让我们退回邮件,发送失败消息“本地交付失败”。

我在做什么错?

回答

1

您需要验证它是否正确解析传入消息。加入这一行下面的邮件()命令来验证脚本的输出:

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

然后通过脚本命令行测试电子邮件,观看屏幕。有些东西可能无法正确解析。