2015-12-23 126 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; 

然後通過腳本命令行測試電子郵件,觀看屏幕。有些東西可能無法正確解析。