将 IMAP 整合到 PHP 应用
ImapEngine 是 Steve Bauman 提供的一个 PHP 包,它提供了一个简单的 API,用于管理没有 PHP IMAP 扩展的邮箱。它提供了一个简单的 API 来管理邮箱,并与 PHP 8.1 及更高版本兼容:
use DirectoryTree\ImapEngine\Mailbox;
$mailbox = new Mailbox([
'port' => 993,
'username' => '...',
'password' => '...',
'encryption' => 'ssl',
'host' => 'imap.example.com',
]);
$inbox = $mailbox->folders()->inbox();
// Get all message UIDs in the inbox.
$messages = $inbox->messages()->get();
// All messages in the last 7 days with a specific subject
$messages = $inbox->messages()
->since(now()->subDays(7))
->subject('Hello World')
->get();
// Get all messages in the inbox with various content enabled.
$messages = $inbox->messages()
->withHeaders() // Enable fetching message headers.
->withFlags() // Enable fetching message flags.
->withBody() // Enable fetching message bodies (including attachments).
->get();
检索邮件后,你可以与它们交互以获取主题、发件人、附件等。甚至可以将邮件标记为已读或移动它:
foreach ($message->attachments() as $attachment) {
// Get the attachment's filename.
$attachment->filename();
// Get the attachment's content type.
$attachment->contentType();
// Get the attachment's contents.
$attachment->contents();
// Get the attachment's extension.
$extension = $attachment->extension();
// Save the attachment to a local file.
$attachment->save("/path/to/save/attachment.$extension");
}
// Mark the message as read.
$message->markSeen();
// Move the message to an "Archive" folder.
$message->move('Archive');
主特性
- 易于进行连接设置:使用简单的配置建立到邮箱的链接
- 目录管理: 检索及管理邮箱目录
- 邮件检索:从邮箱访问和获取邮件
- 信息交互:执行将邮件标记为已读或未读以及删除电子邮件等操作
- 闲置支持:在目录上实时闲置
在 GitHub 上查看 ImapEngine 的源代码,了解安装说明和示例。