How to extract data from POP3 email?

I am receiving email from a POP3 server with PHP. I have a source line containing headers and body. How can I handle the headers and body as a class or array?

An example of what I have:

Return-path: Envelope-to: x@x.de Delivery-date: Wed, 06 Feb 2013 09:35:37 +0100 Received: from mail by host6.toxpress.com with spam-scanned (Exim 4.80.1) (envelope-from ) id 1U30Tc-0003sH-LQ for x@x.de ; Wed, 06 Feb 2013 09:35:36 +0100 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on host6.toxpress.com X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,TVD_SPACE_RATIO autolearn=ham version=3.3.2 Received: from moh3-ve2.go2.pl ([193.17.41.86] helo=moh3-ve1.go2.pl) by host6.toxpress.com with esmtp (Exim 4.80.1) (envelope-from ) id 1U30Tc-0003rk-2b for x@x.de ; Wed, 06 Feb 2013 09:35:32 +0100 Received: from moh3-ve1.go2.pl (unknown [10.0.0.157]) by moh3-ve1.go2.pl (Postfix) with ESMTP id BC734AF611B for ; Wed, 6 Feb 2013 09:34:31 +0100 (CET) Received: from unknown (unknown [10.0.0.108]) by moh3-ve1.go2.pl (Postfix) with SMTP for ; Wed, 6 Feb 2013 09:34:31 +0100 (CET) Received: from 43.dynamic.chello.pl [89.69.42.127] by poczta.o2.pl with ESMTP id jtIGfv; Wed, 06 Feb 2013 09:34:31 +0100 Message-ID: < 51121593.3090008@o2.pl > Date: Wed, 06 Feb 2013 09:34:27 +0100 From: Tomasz Reply-To: tomasz_smykrowski@o2.pl Organization: Tomasz User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Thunderbird/20.0a2 MIME-Version: 1.0 To: x@x.de Subject: adaads Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-O2-Trust: 2, 64 X-O2-SPF: neutral dsadasdsaas . 

And what I want to do:

 echo $from; echo $replyTo; echo $date; echo $body; echo $subject; echo $charset; 
+4
source share
5 answers

Take a look at the MIME message parser class .
According to the description of the author:

This class can be used to parse and decode MIME email messages. It supports decoding of individual RFC 2822 MIME messages or archives that aggregate multiple messages in mbox format. The decoded message data is returned as an array that describes one or more messages found in the message file or data string . Multiple message bodies can be decoded into separate sub-messages. The message body data can be decoded and stored in separate files, so the class can process messages more than the available memory.

+6
source

Yes of course there are: mailparse

http://php.net/manual/en/book.mailparse.php

+2
source

You can slice email into arrays using bounce_driver.class :

https://github.com/cfortune/PHP-Bounce-Handler/blob/master/bounce_driver.class.php

+1
source

I suggest using the IMAP extension, which has a useful function for reading IMAP and POP messages. Open the connection with imap_open , then read the header with imap_headerinfo , it will return an array with all the headers (Subject, To, ...), then use imap_body to get the body of the message. It is easy to parse all messages without using Regex or other functions. You can find a simple example here .

Here is the simple output of imap_headerinfo :

 stdClass Object ( [Date] => 23 Apr 2003 22:10:21 +0100 [Subject] => Top Deals: GBP 15 Gift Certificate Offer and at Least 30% off Recommended Books [message_id] => <.AAA-39045660-13180, 1295.1051132221@mail-ems-103.amazon.com > [toaddress] => xxxx@yyyy.co.uk [to] => Array ([0] => stdClass Object ([mailbox] => xxxx [host] => yyyy.co.uk ) ) [fromaddress] => "Amazon.co.uk" < foryou@amazon.co.uk > [from] => Array ( [0] => stdClass Object ( [personal] => Amazon.co.uk [mailbox] => foryou [host] => amazon.co.uk ) ) [reply_toaddress] => "Amazon.co.uk" < foryou@amazon.co.uk > [reply_to] => Array ( [0] => stdClass Object ( [personal] => Amazon.co.uk [mailbox] => foryou [host] => amazon.co.uk ) ) [senderaddress] => "Amazon.co.uk" < foryou@amazon.co.uk > [sender] => Array ( [0] => stdClass Object ( [personal] => Amazon.co.uk [mailbox] => foryou [host] => amazon.co.uk ) ) [Recent] => [Unseen] => [Flagged] => F [Answered] => [Deleted] => [Draft] => [Msgno] => 250 [MailDate] => 23-Apr-2003 22:22:32 +0100 [Size] => 36375 [udate] => 1051132952 ) 
+1
source

Have you tried the Zend Framework Zend_Mail ?

According to the manual page: * Zend_Mail can read mail messages from several local or remote mail stores. All of them have the same basic API for counting and retrieving messages, and some of them implement additional interfaces for not so common functions. *

http://framework.zend.com/manual/1.12/en/zend.mail.read.html

 $mail = new Zend_Mail_Storage_Pop3(array('host' => 'localhost', 'user' => 'test', 'password' => 'test')); echo $mail->countMessages() . " messages found\n"; foreach ($mail as $message) { echo "Mail from '{$message->from}': {$message->subject}\n"; } 
0
source

All Articles