Page 1 of 1

Mail parsing with PHP

Posted: Tue Jul 08, 2008 11:19 am
by ruj.sabya
I have the "raw" email string: -

Code: Select all

Received: by 10.142.82.7 with HTTP; Tue, 8 Jul 2008 03:25:54 -0700 (PDT)
Message-ID: <a415f8dd0807080325v14f2ee59o7a9a37e51a3bbc6f@mail.gmail.com>
Date: Tue, 8 Jul 2008 15:55:54 +0530
From: "User Name" <user@example.com>
To: "User Name" <user@example.com>
Subject: Hi
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Delivered-To: user@example.com
 
My message
Now, I want to parse the mail. What is the best way to do it?
I was able to do it very efficiently in python's email.Parser class

Re: Mail parsing with PHP

Posted: Wed Jul 09, 2008 4:41 am
by ruj.sabya
I tried using imap_* functions in PHP.
Can anyone suggest how to do it efficiently?
I mean parsing headers, decoding them, decoding body of an email, etc.?

I found PEAR's Mail_Mime() package. But could not find anything there for decoding headers, body, text part of the body, etc. individually. Which I think imap_* functions can do (I just read the documentation, but did not try them).

But I am looking for a single package/solution which is very popular.
I guess this is very common problem to parse mails. And there SHOULD be a standard/popular solution to do this.

Re: Mail parsing with PHP

Posted: Wed Jul 09, 2008 4:50 pm
by m3mn0n
You'll have better luck getting an answer in the 'PHP - Code' forum.

viewforum.php?f=1

Re: Mail parsing with PHP

Posted: Wed Jul 09, 2008 4:59 pm
by Eran
I suggest Zend_Mail - http://framework.zend.com/manual/en/zend.mail.html, though its mail parsing implementation is only documented in the code itself
Create a Zend_Mail_Message object from your mail string -

Code: Select all

 
$message = new Zend_Mail_Message(array('raw' =>  $email)); //$email is your message in string form
 
And then you can manipulate it using the Zend_Mail_Message/Part API