dqu92800 2015-12-23 18:33
浏览 59
已采纳

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

I am trying to write a script which will be used to get emails piped to it and then forwarded to another email address with a different "From" field

#!/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("
", $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]."
";

// 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]."
";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}

mail( "emailTo@example.com", $subject,$message, $from );

?>

this script is getting us bounced emails with delivery failure message "local delivery failed".

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • duan1979768678 2015-12-23 18:49
    关注

    You need to verify that it's parsing the incoming messages properly. Add this line following your mail() command to verify the script's output:

    echo "Subject:".$subject."
    From:".$from."
    Message:".$message;
    

    Then pass the script a test email from the command line and watch the screen. Something probably isn't getting parsed correctly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分