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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化