Best way to handle email parsing/decoding in PHP?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 20:29:37

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 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 :)

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!