title: email pipe to PHP script and forward to another email
body: <p>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</p>
  
  <pre><code>#!/usr/local/bin/php56 -q
  &lt;?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 = emailFrom@example.com;
  $subject = "";
  $headers = "";
  $message = "";
  $is_header= true;
  
  //loop through each line
  for ($i=0; $i &lt; 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( "emailTo@example.com", $subject,$message, $from );
  
  ?&gt;
  </code></pre>
  
  <p>this script is getting us bounced emails with delivery failure message "local delivery failed".</p>
  
  <p>What am I doing wrong?</p>