divineomega / email-structure-parser
Email 结构解析器
v2.2.0
2021-02-12 12:33 UTC
Requires
- ext-imap: *
This package is auto-updated.
Last update: 2024-09-12 19:55:36 UTC
README
给定一个 IMAP 流和消息编号,这个库将解析多部分电子邮件的结构。
安装
您可以使用以下 Composer 命令轻松安装 Email Structure Parser 包。
composer require divineomega/email-structure-parser
用法
要使用此解析器,您必须首先使用 PHP 内置的 imap_open
函数连接到邮件服务器。连接后,您需要通过 imap_search
函数检索消息编号。
一旦您有了消息编号,您可以将它和 IMAP 流对象一起传递给 EmailStructureParser
。然后您可以通过调用 getParts()
方法获取一个按 MIME 类型拆分的解析电子邮件部分的数组。
请参阅以下示例用法代码。
use DivineOmega\EmailStructureParser\EmailStructureParser; // Connect to mailbox $imapStream = imap_open('{outlook.office365.com:993/ssl/novalidate-cert}INBOX', getenv('USERNAME'), getenv('PASSWORD')); // Get a message number (in this example, just get the first) $msgNums = imap_search($imapStream, 'ALL'); $msgNum = $msgNums[0]; // Load message into parser $parser = new EmailStructureParser($imapStream, $msgNum); // Get parsed multipart email parts - including plain text and/or HTML content, and any attachments $parts = $parser->getParts(); // Output HTML email content var_dump($parts['TEXT/HTML']); // Save attached PNG images foreach($parts['IMAGE/PNG'] as $image) { file_put_contents($image->name, $image->content); }
内容 ID
一些电子邮件使用 cid:
URL 将图像嵌入到 HTML 内容中。这些 URL 链接到电子邮件中另一部分的 内容 ID,而不是可以正常解析的绝对 https://
URL。
如果存在,则此内容 ID 将通过 Part
的 contentId
属性暴露。
foreach($parts['IMAGE/PNG'] as $image) { // Store the file as in the above example: file_put_contents($image->name, $image->content); // You would then want to store this relationship in your database: echo "{$image->contentId} => {$image->name}\n"; }