Best way to handle email parsing/decoding in PHP?
Currently I'm using the PEAR library's mimeDecode.php for parsing incoming emails. It seems to have a lot of issues and fails to decode a lot of messages, so I'd like to replace it with something better.
I'm looking for something that is able to properly separate parts of the message, such as to, from, body, etc. Ideally it would be able to handle all common encoding methods such as base64, uuencode, quoted printable, etc.
In situations where both plain text and html versions of the same message are contained in a single email, I would ideally like it to know the difference between them so I could choose which part I wished to display.
I'm not worried about attachments at this point in time, but it would be nice for it to have knowledge of them in case I want to implement that in the future.
I saw PHP has a group of functions that start with the word imap that appear they may do what I would like, but I am unsure without trying them out.
Currently I am doing on-the-fly decoding of the messages in PHP, which is why I am looking for a PHP replacement solution.
Does anyone have an expe开发者_Go百科rience with this that could point me in the right direction? I'd hate to start using something that would end up not doing what I need in the long run.
I have recently developed a PHP mail parser and I have been using it on production.
I have very happy with it and some developers has forked it:
https://github.com/plancake/official-library-php-email-parser
I know this question's four years old now... but I ended up in need of a mail parsing library and wasn't satisfied with any of the available options. I wanted something reliable, PSR-2 compliant, installable via composer.
composer require zbateson/mail-mime-parser
It's its own parser, built from the ground up to get around known issues and bugs in other implementations. It is extensively tested and quite widely used.
The library makes use of Psr7 streams which allow you to pass it any kind of stream you like. It also doesn't store all information in memory -- very large attachments can be returned as a stream instead of a string if so desired, so memory isn't used up. Similarly the entire message is never stored directly in memory, only references to streams, and headers are kept in-memory.
https://github.com/zbateson/mail-mime-parser
Check out the website for a guide and the API... and if you find bugs/typos or see improvements, please feel free to open an issue, or dig right in and contribute with a pull request :)
Funny you should ask... Im actually working on a simple notification system now. I just finished up the Bounce Manager with i use Zend_Mail to implement. It has pretty much all the features you're looking for... you can connect to a mailbox (POP3, IMAP, Mbox, and Maildir) and pull messages from it as well as operate on all those messages.
It handles multipart messages, but the parts can be hard to work with. I had a hard time figuring out which part was the attached original message part in the NDR's I was working with, but I have a feeling I just missed something in the documentation. I'm not sure how it handles encoding, because my usage was fairly simple but I'm pretty sure it has provisions for all the encodings you mentioned. Check out the docs and browse the API.
I forked the php-mime-mail-parser to correct all the issues : Fork of php-mime-mail-parser
More than 52 tests and 764 assertions Code Coverage : 100% lines, 100% Functions and Methods, 100% Classes and Traits
You need the PECL Package MailParse to use it but the wrapper is without issue and fully tested.
For completeness here's the one I'm going to try. http://code.google.com/p/php-mime-mail-parser/ - it's a wrapper around PHP MailParse, which needs to be installed.
I'm currently also on the lookout for an easy to use, robust MIME email parsing library and am currently seriously looking into Mail component from eZ Components. But, if you're looking for something that will make it as easy as echo $email->text;
or echo $email->html;
, like I was, you'll be disappointed. Actually, now I don't think such simplification is even possible, due to the way MIME works. But it does seem like the best option out there in the PHP world.
I started working on my current project with Zend_Mail component, but when the time came to actually dig inside those email parts and encoded headers, Zend_Mail pretty much leaves you out in the cold. You need to do most decoding yourself, which is not fun at all.
As for IMAP PHP extension, its meant to deal with retrieving messages from your mailbox, not MIME decoding them. Although, it does have some handy decoding function that you might need. Mailparse PECL extension, on the other hand, deals with exactly that problem set. I haven't tried it yet, but it seems like you need to write a lot of code to actually get to the data you want.
精彩评论