I have the following script that I pipe an email in cPanel to:
#!/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 );
?>
The email I expect to receive at the new address never reaches the destination.
Just as a test I tried uploading this code to another file:
<?php
mail( "Me@MyDomain.com", "Boo","Blah", "From: Someone@Somewhere.com\r\n" );
die("Mail Sent");
?>
I access the above code from the browser and it works fine. If I change the code to the below, and pipe an email to it, no email is received:
#!/usr/bin/php -q
<?php
mail( "Me@MyDomain.com", "Boo","Blah", "From: Someone@Somewhere.com\r\n" );
?>
I believe if I could get the small test to work, I could get the original code to work. Is there something I am doing wrong with the pipe? It is worth noting that in the original code, if I uncomment the debugging code, I get a message failure message and all the variables in the debug code are correct.