rdx / imap
从 IMAP/POP3 源读取并解析电子邮件
2.8
2020-04-20 10:49 UTC
Requires
- ext-imap: *
README
在 IMAP 服务器上读取电子邮件。
特性
- 检索消息部分
- 识别 PLAIN & HTML 部分
- 识别附件
使用 PHP 内置的 IMAP 模块。
示例
初始化连接并查找消息
$mbox = new rdx\imap\IMAPMailbox('example.com', 'user', 'password', 'INBOX', ['ssl', 'tls']);
$messages = $mbox->messages([
'newestFirst' => true, // bool
'seen' => false, // null|bool
'limit' => 10, // int
'offset' => 0, // int
]);
查看消息的结构
foreach ($messages as $message) {
echo $message->simpleStructure() . "\n\n";
// Could be something complex like:
// 1. PLAIN (517)
// 2. DELIVERY-STATUS (315)
// 3. *RFC822 (2446)
// 3.1. PLAIN (610)
// 3.2. HTML (744)
// Or something simple like:
// 1. PLAIN (123)
// 2. JPEG (76543)
}
查找所有 HTML 部分,包括附件、转发等
foreach ($messages as $message) {
$htmls = $message->html(true); // true for recursive, false for only top level parts
}
读取退信邮件以查找被拒绝的地址
foreach ($messages as $message) {
$body = $message->subtypeContent('DELIVERY-STATUS');
if ($body && strpos($body, 'failed') !== false) {
// Extract address and do something
}
}
查找所有图像附件
foreach ($messages as $message) {
$attachments = $message->subtypeParts(['JPEG', 'PNG', 'GIF'], true); // true = recursive
foreach ($attachments as $att) {
$att->saveAttachment('/some/folder');
}
}