如何从PHP中的各种电子邮件源获取XML / TXT附件?

[英]How to get XML/TXT attachments from diverse email sources in PHP?


Once the SMTP/MIME text is obtained, it is hard to tell how complex it can be to parse/handle emails coming from all kind of random SMTP/MIME programs.

获得SMTP / MIME文本后,很难说解析/处理来自各种随机SMTP / MIME程序的电子邮件有多复杂。

For the particular pose of this question, obtaining header information is completly irrelevant. Only attachments are needed.

对于该问题的特定姿势,获得标题信息是完全不相关的。只需要附件。

¿Is out there any bullet proof solution for this?

¿有没有针对此的防弹解决方案?

Thank you

1 个解决方案

#1


You will need a MIME parser to process MIME data. In PHP, the Mailparse extension can do this for you. It's a fairly basic library, so a wrapper like php-mime-mail-parser can greatly simplify things. In any case, assuming Mailparse, the basic outline of the process is the following: load/parse the email, then enumate all parts and subparts where the following conditions are met:

您将需要一个MIME解析器来处理MIME数据。在PHP中,Mailparse扩展可以为您执行此操作。它是一个相当基础的库,所以像php-mime-mail-parser这样的包装器可以大大简化一些事情。在任何情况下,假设Mailparse,该过程的基本概要如下:加载/解析电子邮件,然后启用满足以下条件的所有部分和子部分:

  • The Content-Disposition header for the part is attachment.
  • 零件的Content-Disposition标头是附件。

  • The decoded filename parameter of the Content-Dispositon header ends with the literal .xml or .txt. Alternatively, you can rely on the Content-Type header for type detection (text/XML and text/plain, respectively), but that will be less reliable, because it depends on the email sender finding out the right MIME type for the attached document.
  • Content-Dispositon标头的已解码文件名参数以文字.xml或.txt结尾。或者,您可以依赖Content-Type标头进行类型检测(分别是text / XML和text / plain),但这样会不太可靠,因为它依赖于电子邮件发件人找到所附文档的正确MIME类型。

Once the attachments parts are found, they can be decoded from MIME representation using the PHP-provided functions for BASE64 or quoted-printable decoding.

找到附件部分后,可以使用PHP提供的BASE64函数或引用可打印解码从MIME表示中对它们进行解码。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/08/1908484838eebc56881cf45804b846d3.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com