使用PIPE转发邮件到另一个地址

时间:2017-10-21 01:11:21

标签: php bash email

我有以下脚本,我将cPanel中的电子邮件发送到:

#!/usr/bin/php -q
<?php

// Specify where emails should be forwarded to and who it should come from
$new_email_recipient="Me@MyDomain.com";
$new_email_sender="Someone <Someone@Somewhere.ca>";

// Define the new mail headers
$additional_header="To: ".$new_email_recipient."\r\n";
$additional_header.="From: ".$new_email_sender."\r\n";




$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 variables which will assigned later on
$from = "";
$subject = "";
$headers = "";
$message = "";
$is_header= true;

//loop through each line
for ($i=0; $i < count($lines); $i++)
{
    if ($is_header)
    {
        // Add header lines to the headers variable
        $headers .= $lines[$i]."\r\n";

        // Split out the subject portion
        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches))
        {
            $subject = $matches[1];
        }
        // Split out the recipient portion
        if (preg_match("/^To: (.*)/", $lines[$i], $matches))
        {
            $to = $matches[1];
        }
        //Split out the sender information portion
        if (preg_match("/^From: (.*)/", $lines[$i], $matches))
        {
            $from = $matches[1];
        }
    }
    else
    {
        /// Add message lines to the message variable
        $message .= $lines[$i]."\r\n";
    }

    if (trim($lines[$i])=="")
    {
        // empty line, header section has ended
        $is_header = false;
    }
}

//Prepend the original sender and original recipient to the beginning of the message
$message="From: ".$from."\r\n"."To: ".$to."\r\n"."\r\n".$message;

//For debugging puposes, echo the values to ensure they are correct.
//echo "New Recipient:".$new_email_recipient."\nNew Sender:".$new_email_sender."\n";
//echo "Subject:".$subject."\nFrom:".$from."\nMessage:".$message."\nHeaders:".$additional_header;

//Send a copy of the message to the new recipient
mail( $new_email_recipient, $subject,$message, $additional_header );

?>

我希望在新地址收到的电子邮件永远不会到达目的地。

正如测试一样,我尝试将此代码上传到另一个文件:

<?php

mail( "Me@MyDomain.com", "Boo","Blah", "From: Someone@Somewhere.com\r\n" );
die("Mail Sent");

?>

我从浏览器访问上面的代码,它工作正常。 如果我将代码更改为以下代码并向其发送电子邮件,则不会收到任何电子邮件:

#!/usr/bin/php -q
<?php
mail( "Me@MyDomain.com", "Boo","Blah", "From: Someone@Somewhere.com\r\n" );

?>

我相信如果我能让小测试工作,我可以让原始代码工作。管道上有什么问题吗?值得注意的是,在原始代码中,如果我取消注释调试代码,我会收到消息失败消息,并且调试代码中的所有变量都是正确的。

0 个答案:

没有答案