Vaibhav
Vaibhav

Reputation: 71

email pipe to PHP script and forward to another email

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("\n", $message);

// initialize variable which will assigned later on
$from = [email protected];
$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]."\n";

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

mail( "[email protected]", $subject,$message, $from );

?>

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

What am I doing wrong?

Upvotes: 3

Views: 2218

Answers (1)

cantelope
cantelope

Reputation: 1165

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."\nFrom:".$from."\nMessage:".$message;

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

Upvotes: 1

Related Questions