Mail parsing with PHP

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
ruj.sabya
Forum Newbie
Posts: 2
Joined: Tue Jul 08, 2008 11:15 am

Mail parsing with PHP

Post 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
ruj.sabya
Forum Newbie
Posts: 2
Joined: Tue Jul 08, 2008 11:15 am

Re: Mail parsing with PHP

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Re: Mail parsing with PHP

Post by m3mn0n »

You'll have better luck getting an answer in the 'PHP - Code' forum.

viewforum.php?f=1
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Mail parsing with PHP

Post 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
Post Reply