0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
G-J
  • 1,080
  • 2
  • 16
  • 32
  • Isn't `#!/usr/bin/php -q` supposed to be the first line? – Funk Forty Niner Oct 21 '17 at 01:15
  • @Fred-ii- Yes you are correct. My actual code had it right, I just messed it up when I put it here. I corrected the code in the original post. Still confused though. – G-J Oct 21 '17 at 01:24
  • *Hm....*, I never used bash scripts before so I may not be the right person for this, but does anything show up in server logs? – Funk Forty Niner Oct 21 '17 at 01:43
  • See if any of these Q&A's help https://stackoverflow.com/q/5155923/1415724 https://stackoverflow.com/q/16700135/1415724 and where both use `mail -s` so it could be that, and/or something else. Command line syntax is different, so it seems. – Funk Forty Niner Oct 21 '17 at 01:52

0 Answers0