title: Email piping with php script to collect the TO field?
body: <p>I want to forward my bounced emails to a php script to deal with them.  I am using.</p>
  
  <pre><code> #!/usr/bin/php -q
   &lt;?php
  
   // read from stdin
   $fd = fopen("php://stdin", "r");
   $email = "";
   while (!feof($fd)) {
  $email .= fread($fd, 1024);
   }
     fclose($fd);
  
     // handle email
     $lines = explode("\n", $email);
  
    // empty vars
     $from = "";
      $subject = "";
      $headers = "";
      $message = "";
      $splittingheaders = true;
  
      for ($i=0; $i &lt; count($lines); $i++) {
      if ($splittingheaders) {
      // this is a header
      $headers .= $lines[$i]."\n";
  
      // look out for special headers
      if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
          $subject = $matches[1];
      }
      if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
          $from = $matches[1];
      }
  } else {
      // not a header, but message
      $message .= $lines[$i]."\n";
  }
  
  if (trim($lines[$i])=="") {
      // empty line, header section has ended
      $splittingheaders = false;
     }
     }
  
    ?&gt;     
  </code></pre>
  
  <p>Works perfect!  But how do I collect the "To" field in the bounced message?  I've tried just adding a $to variable but it doesn't work.  </p>
  
  <p>Any help would be great, </p>
  
  <p>thanks,</p>
  
  <p>EDIT:  Actually I need to get the "TO" field within the body of the message. - The email that it bounced back from.  How do I pull apart the body of the message to take specific info?  Should I create a special header with the person's email so that it is easier to get this info?</p>