title: How do I get just the text content from a multipart email?
body: <pre><code>    #!/usr/bin/php -q
      &lt;?php
      $savefile = "savehere.txt";
      $sf = fopen($savefile, 'a') or die("can't open file");
      ob_start();
  
      // 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];
              }
              if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
                  $to = $matches[1];
              }
          } else {
              // not a header, but message
              $message .= $lines[$i]."\n";
  
  
  
  
          }
  
          if (trim($lines[$i])=="") {
              // empty line, header section has ended
              $splittingheaders = false;
          }
      }
  /*$headers is ONLY included in the result at the last section of my question here*/
      fwrite($sf,"$message");
      ob_end_clean();
      fclose($sf);
      ?&gt;
  </code></pre>
  
  <p>That is an example of my attempt. The problem is I am getting too much in the file.
  Here is what is being written to the file: (I just sent a bunch of garbage to it as you can see)</p>
  
  <pre><code>From xxxxxxxxxxxxx Tue Sep 07 16:26:51 2010
  Received: from xxxxxxxxxxxxxxx ([xxxxxxxxxxx]:3184 helo=xxxxxxxxxxx)
      by xxxxxxxxxxxxx with esmtpa (Exim 4.69)
      (envelope-from &lt;xxxxxxxxxxxxxxxx&gt;)
      id 1Ot4kj-000115-SP
      for xxxxxxxxxxxxxxxxxxx; Tue, 07 Sep 2010 16:26:50 -0400
  Message-ID: &lt;EE3B7E26298140BE8700D9AE77CB339D@xxxxxxxxxxx&gt;
  From: "xxxxxxxxxxxxx" &lt;xxxxxxxxxxxxxx&gt;
  To: &lt;xxxxxxxxxxxxxxxxxxxxx&gt;
  Subject: stackoverflow is helping me
  Date: Tue, 7 Sep 2010 16:26:46 -0400
  MIME-Version: 1.0
  Content-Type: multipart/alternative;
      boundary="----=_NextPart_000_0169_01CB4EA9.773DF5E0"
  X-Priority: 3
  X-MSMail-Priority: Normal
  Importance: Normal
  X-Mailer: Microsoft Windows Live Mail 14.0.8089.726
  X-MIMEOLE: Produced By Microsoft MimeOLE V14.0.8089.726
  
  This is a multi-part message in MIME format.
  
  ------=_NextPart_000_0169_01CB4EA9.773DF5E0
  Content-Type: text/plain;
      charset="iso-8859-1"
  Content-Transfer-Encoding: quoted-printable
  
  111
  222
  333
  444
  ------=_NextPart_000_0169_01CB4EA9.773DF5E0
  Content-Type: text/html;
      charset="iso-8859-1"
  Content-Transfer-Encoding: quoted-printable
  
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;
  &lt;HTML&gt;&lt;HEAD&gt;
  &lt;META content=3Dtext/html;charset=3Diso-8859-1 =
  http-equiv=3DContent-Type&gt;
  &lt;META name=3DGENERATOR content=3D"MSHTML 8.00.6001.18939"&gt;&lt;/HEAD&gt;
  &lt;BODY style=3D"PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: =
  15px"=20
  id=3DMailContainerBody leftMargin=3D0 topMargin=3D0 =
  CanvasTabStop=3D"true"=20
  name=3D"Compose message area"&gt;
  &lt;DIV&gt;&lt;FONT face=3DCalibri&gt;111&lt;/FONT&gt;&lt;/DIV&gt;
  &lt;DIV&gt;&lt;FONT face=3DCalibri&gt;222&lt;/FONT&gt;&lt;/DIV&gt;
  &lt;DIV&gt;&lt;FONT face=3DCalibri&gt;333&lt;/FONT&gt;&lt;/DIV&gt;
  &lt;DIV&gt;&lt;FONT face=3DCalibri&gt;444&lt;/FONT&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;
  
  ------=_NextPart_000_0169_01CB4EA9.773DF5E0--
  </code></pre>
  
  <p>I found this while searching around but have no idea how to implement or where to insert in my code or if it would work.</p>
  
  <pre><code>preg_match("/boundary=\".*?\"/i", $headers, $boundary);
  $boundaryfulltext = $boundary[0];
  
  if ($boundaryfulltext!="")
  {
  $find = array("/boundary=\"/i", "/\"/i");
  $boundarytext = preg_replace($find, "", $boundaryfulltext);
  $splitmessage = explode("--" . $boundarytext, $message);
  $fullmessage = ltrim($splitmessage[1]);
  preg_match('/\n\n(.*)/is', $fullmessage, $splitmore);
  
  if (substr(ltrim($splitmore[0]), 0, 2)=="--")
  {
  $actualmessage = $splitmore[0];
  }
  else
  {
  $actualmessage = ltrim($splitmore[0]);
  }
  
  }
  else
  {
  $actualmessage = ltrim($message);
  }
  
  $clean = array("/\n--.*/is", "/=3D\n.*/s");
  $cleanmessage = trim(preg_replace($clean, "", $actualmessage)); 
  </code></pre>
  
  <p>So, how can I get just the plain text area of the email into my file or script for furthr handling??</p>
  
  <p>Thanks in advance. stackoverflow is great!</p>