doni49 Posted February 18, 2007 Share Posted February 18, 2007 I've been searching for the past 2 hours for this and I can't find it. I'm trying to figure out how to parse email messages in PHP. I'm piping an email address to a PHP script. I've got it so that it recieves the messages in their entirety. But I'm having an impossible time figuring out how to break it down in to the headers, body and attachment(s). Here's a sample of what my script sees when it runs (this is all in one variable). Thanks! From listservName-return-49832-myemail=mydomain.com@listservDomain.net Sun Feb 18 01:29:34 2007 Received: from [207.7.198.3] (helo=richard2.listservDomain.net) by server125.chihost.com with smtp (Exim 4.63) (envelope-from <listservName-return-49832-myemail=mydomain.com@listservDomain.net>) id 1HIgUe-0004e8-R5 for [email protected]; Sun, 18 Feb 2007 01:29:33 -0600 Received: (qmail 22925 invoked by alias); 18 Feb 2007 07:30:33 -0000 Mailing-List: contact [email protected]; run by ezmlm Reply-To: listservName <[email protected]> Precedence: bulk X-No-Archive: yes list-help: <mailto:[email protected]> list-unsubscribe: <mailto:[email protected]> list-post: <mailto:[email protected]> Delivered-To: mailing list [email protected] Received: (qmail 22914 invoked from network); 18 Feb 2007 07:30:33 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.7 (2006-10-05) on mail.listservDomain.net X-Spam-Status: No, score=-3.0 required=6.0 tests=BAYES_95,UNPARSEABLE_RELAY, USER_IN_WHITELIST_TO autolearn=no version=3.1.7 X-Spam-Level: Message-ID: <[email protected]> Date: Sun, 18 Feb 2007 02:30:17 -0500 From: Don lastName <[email protected]> User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 To: listservName <[email protected]> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Test--Please disregard Please disregard this message--I've been having email troubles and I'm trying to troubleshoot. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] Advertising is prohibited Link to comment https://forums.phpfreaks.com/topic/39010-parse-email-message/ Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 This is code I found a few years ago: <?php $hMail=fopen("php://stdin", "r"); $email=""; while (!feof($hMail)) $email.=fread($hMail, 1024); fclose($hMail); // handle email $lines = explode("\n", $email); // empty vars $from = ""; $subject = ""; $headers = ""; $message = ""; $body = array(); $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]; $body[] = 'Subject: ' . $subject; } if (preg_match("/^From: (.*)/", $lines[$i], $matches)) { $from = $matches[1]; $body[] = 'From: ' . $from; } } else { // not a header, but message $message .= $lines[$i]."\n"; } if (trim($lines[$i])=="") { // empty line, header section has ended $splittingheaders = false; } } ?> It should give you enough to work with. Ken Link to comment https://forums.phpfreaks.com/topic/39010-parse-email-message/#findComment-187892 Share on other sites More sharing options...
superuser2 Posted February 18, 2007 Share Posted February 18, 2007 Thanks. That should be useful for me, as well. Link to comment https://forums.phpfreaks.com/topic/39010-parse-email-message/#findComment-188021 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.