How do I get just the text content from a multipart email?

LadyB

New Member
\[code\] #!/usr/bin/php -q <?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 < 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); ?>\[/code\]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)\[code\]From xxxxxxxxxxxxx Tue Sep 07 16:26:51 2010Received: from xxxxxxxxxxxxxxx ([xxxxxxxxxxx]:3184 helo=xxxxxxxxxxx) by xxxxxxxxxxxxx with esmtpa (Exim 4.69) (envelope-from <xxxxxxxxxxxxxxxx>) id 1Ot4kj-000115-SP for xxxxxxxxxxxxxxxxxxx; Tue, 07 Sep 2010 16:26:50 -0400Message-ID: <EE3B7E26298140BE8700D9AE77CB339D@xxxxxxxxxxx>From: "xxxxxxxxxxxxx" <xxxxxxxxxxxxxx>To: <xxxxxxxxxxxxxxxxxxxxx>Subject: stackoverflow is helping meDate: Tue, 7 Sep 2010 16:26:46 -0400MIME-Version: 1.0Content-Type: multipart/alternative; boundary="----=_NextPart_000_0169_01CB4EA9.773DF5E0"X-Priority: 3X-MSMail-Priority: NormalImportance: NormalX-Mailer: Microsoft Windows Live Mail 14.0.8089.726X-MIMEOLE: Produced By Microsoft MimeOLE V14.0.8089.726This is a multi-part message in MIME format.------=_NextPart_000_0169_01CB4EA9.773DF5E0Content-Type: text/plain; charset="iso-8859-1"Content-Transfer-Encoding: quoted-printable111222333444------=_NextPart_000_0169_01CB4EA9.773DF5E0Content-Type: text/html; charset="iso-8859-1"Content-Transfer-Encoding: quoted-printable<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><META content=3Dtext/html;charset=3Diso-8859-1 =http-equiv=3DContent-Type><META name=3DGENERATOR content=3D"MSHTML 8.00.6001.18939"></HEAD><BODY style=3D"PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: =15px"=20id=3DMailContainerBody leftMargin=3D0 topMargin=3D0 =CanvasTabStop=3D"true"=20name=3D"Compose message area"><DIV><FONT face=3DCalibri>111</FONT></DIV><DIV><FONT face=3DCalibri>222</FONT></DIV><DIV><FONT face=3DCalibri>333</FONT></DIV><DIV><FONT face=3DCalibri>444</FONT></DIV></BODY></HTML>------=_NextPart_000_0169_01CB4EA9.773DF5E0--\[/code\]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.\[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\]So, how can I get just the plain text area of the email into my file or script for furthr handling??Thanks in advance. stackoverflow is great!
 
Back
Top