在 PHP 中处理电子邮件解析/解码的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4721410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to handle email parsing/decoding in PHP?
提问by Sgraffite
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.
目前我正在使用 PEAR 库的 mimeDecode.php 来解析传入的电子邮件。它似乎有很多问题并且无法解码很多消息,所以我想用更好的东西替换它。
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.
我正在寻找能够正确分离消息部分的东西,例如 to、from、body 等。理想情况下,它能够处理所有常见的编码方法,例如 base64、uuencode、quoted printable 等。
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.
在同一邮件的纯文本和 html 版本都包含在一封电子邮件中的情况下,我希望它知道它们之间的区别,以便我可以选择我希望显示的部分。
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.
我看到 PHP 有一组以单词 imap 开头的函数,它们似乎可以做我想做的事,但我不确定不尝试一下。
Currently I am doing on-the-fly decoding of the messages in PHP, which is why I am looking for a PHP replacement solution.
目前我正在对 PHP 中的消息进行即时解码,这就是我寻找 PHP 替代解决方案的原因。
Does anyone have an experience 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.
有没有人有这方面的经验可以为我指明正确的方向?我不想开始使用从长远来看最终不会做我需要的东西。
采纳答案by prodigitalson
Funny you should ask... Im actually working on a simple notification system now. I just finished up the Bounce Manager with i use Zend_Mailto 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.
有趣的是你应该问......我现在实际上正在研究一个简单的通知系统。我刚刚完成了弹跳管理器,我使用Zend_Mail来实现。它几乎具有您正在寻找的所有功能……您可以连接到邮箱(POP3、IMAP、Mbox 和 Maildir)并从中提取邮件以及对所有这些邮件进行操作。
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.
它处理多部分消息,但这些部分可能很难处理。我很难弄清楚哪个部分是我正在使用的 NDR 中附加的原始消息部分,但我感觉我只是错过了文档中的某些内容。我不确定它是如何处理编码的,因为我的用法相当简单,但我很确定它对你提到的所有编码都有规定。查看文档并浏览 API。
回答by dan
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:
我最近开发了一个 PHP 邮件解析器,我一直在生产中使用它。
我对它非常满意,一些开发人员已经对它进行了分叉:
https://github.com/plancake/official-library-php-email-parser
https://github.com/plancake/official-library-php-email-parser
回答by Zaahid
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.
我知道这个问题已经四年了……但我最终需要一个邮件解析库并且对任何可用选项都不满意。我想要一些可靠的、符合 PSR-2 的、可通过 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.
该库使用 Psr7 流,允许您将其传递给您喜欢的任何类型的流。它也不会将所有信息存储在内存中——如果需要,非常大的附件可以作为流而不是字符串返回,因此内存不会被用完。类似地,整个消息从不直接存储在内存中,只有对流的引用,并且标头保存在内存中。
https://github.com/zbateson/mail-mime-parser
https://github.com/zbateson/mail-mime-parser
Check out the websitefor 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 :)
查看网站以获取指南和 API ......如果你发现错误/错别字或看到改进,请随时打开一个问题,或直接挖掘并通过拉取请求做出贡献:)
回答by eXorus
I forked the php-mime-mail-parser to correct all the issues : Fork of php-mime-mail-parser
我分叉了 php-mime-mail-parser 来纠正所有问题:php-mime-mail-parser 的分叉
More than 52 tests and 764 assertions Code Coverage : 100% lines, 100% Functions and Methods, 100% Classes and Traits
超过 52 个测试和 764 个断言代码覆盖率:100% 行,100% 函数和方法,100% 类和特征
You need the PECL Package MailParse to use it but the wrapper is without issue and fully tested.
您需要 PECL Package MailParse 才能使用它,但包装器没有问题并且经过全面测试。
回答by Slawa
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.
为了完整起见,这是我要尝试的。http://code.google.com/p/php-mime-mail-parser/- 它是 PHP MailParse 的包装器,需要安装。
回答by nnc
I'm currently also on the lookout for an easy to use, robust MIME email parsing library and am currently seriously looking into Mailcomponent 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.
我目前也在寻找易于使用、强大的 MIME 电子邮件解析库,并且目前正在认真研究eZ Components 的Mail组件。但是,如果您正在寻找像echo $email->text;
或echo $email->html;
一样简单的东西,就像我一样,您会感到失望。实际上,由于 MIME 的工作方式,现在我认为这种简化是不可能的。但它似乎确实是 PHP 世界中最好的选择。
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.
我开始使用 Zend_Mail 组件处理我当前的项目,但是当真正深入挖掘这些电子邮件部分和编码的标题时,Zend_Mail 几乎让您感到寒冷。您需要自己进行大部分解码,这一点都不有趣。
As for IMAPPHP 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. MailparsePECL 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.
至于IMAPPHP 扩展,它的目的是处理从您的邮箱中检索消息,而不是 MIME 解码它们。虽然,它确实有一些您可能需要的方便的解码功能。另一方面,MailparsePECL 扩展正好处理该问题集。我还没有尝试过,但似乎您需要编写大量代码才能真正获取所需的数据。