wisesight / imap
面向对象的PHP IMAP - 由Wisesight修改
1.11.0
2020-11-30 14:52 UTC
Requires
- php: ^7.3 || ^8.0
- ext-iconv: *
- ext-imap: *
- ext-mbstring: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16.7
- laminas/laminas-mail: ^2.12.3
- phpstan/phpstan: ^0.12.57
- phpstan/phpstan-phpunit: ^0.12.16
- phpstan/phpstan-strict-rules: ^0.12.5
- phpunit/phpunit: ^9.4.3
- dev-master
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.0
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2
- 0.1
- dev-feature/coerce-windows-874
This package is auto-updated.
Last update: 2024-09-28 00:37:25 UTC
README
PHP 5.4+ 库,用于通过IMAP读取和处理电子邮件。
安装
确保已安装 PHP IMAP 扩展。例如,在Debian上
# apt-get install php5-imap
推荐通过 Composer 安装IMAP库
$ composer require ddeboer/imap
此命令要求您全局安装Composer,如Composer文档的安装章节所述。
使用方法
连接和认证
use Wisesight\Imap\Server; $server = new Server('imap.gmail.com'); // $connection is instance of \Wisesight\Imap\Connection $connection = $server->authenticate('my_username', 'my_password');
选项
您可以指定端口、标志和参数到服务器
$server = new Server( $hostname, // required $port, // defaults to 993 $flags, // defaults to '/imap/ssl/validate-cert' $parameters );
邮箱
从邮件服务器检索邮箱(也称为邮件文件夹)并遍历它们
$mailboxes = $connection->getMailboxes(); foreach ($mailboxes as $mailbox) { // $mailbox is instance of \Wisesight\Imap\Mailbox printf('Mailbox %s has %s messages', $mailbox->getName(), $mailbox->count()); }
或检索特定的邮箱
$mailbox = $connection->getMailbox('INBOX');
删除邮箱
$mailbox->delete();
消息
从邮箱中检索消息(电子邮件)并遍历它们
$messages = $mailbox->getMessages(); foreach ($messages as $message) { // $message is instance of \Wisesight\Imap\Message }
搜索消息
use Wisesight\Imap\SearchExpression; use Wisesight\Imap\Search\Email\To; use Wisesight\Imap\Search\Text\Body; $search = new SearchExpression(); $search->addCondition(new To('me@here.com')) ->addCondition(new Body('contents')) ; $messages = $mailbox->getMessages($search);
消息属性和操作
获取消息编号和唯一的 消息ID,形式为 <...>
$message->getNumber(); $message->getId();
获取其他消息属性
$message->getSubject(); $message->getFrom(); $message->getTo(); $message->getDate(); $message->isAnswered(); $message->isDeleted(); $message->isDraft(); $message->isSeen();
获取消息头,作为 \Wisesight\Imap\Message\Headers 对象
$message->getHeaders();
获取消息正文,作为HTML或纯文本
$message->getBodyHtml(); $message->getBodyText();
读取消息正文会将消息标记为已读。如果您想保留消息未读状态
$message->keepUnseen()->getBodyHtml();
将消息移动到另一个邮箱
$mailbox = $connection->getMailbox('another-mailbox'); $message->move($mailbox);
删除消息
$mailbox->getMessage(1)->delete(); $mailbox->getMessage(2)->delete(); $mailbox->expunge();
消息附件
获取消息附件(包括内联和附加)并遍历它们
$attachments = $message->getAttachments(); foreach ($attachments as $attachment) { // $attachment is instance of \Wisesight\Imap\Message\Attachment }
将消息附件下载到本地文件
// getDecodedContent() decodes the attachment’s contents automatically: file_put_contents( '/my/local/dir/' . $attachment->getFilename(), $attachment->getDecodedContent() );
运行测试
此库在Travis CI上针对Gmail IMAP服务器进行了功能测试。
如果您有自己的IMAP(测试)帐户,您可以通过提供您的IMAP(例如,Gmail)凭据在本地运行测试
$ composer install --dev $ EMAIL_USERNAME="your_username" EMAIL_PASSWORD="your_password" vendor/bin/phpunit
您还可以设置一个 EMAIL_SERVER
变量,默认为 imap.gmail.com
$ EMAIL_USERNAME="your_username" EMAIL_PASSWORD="your_password" EMAIL_SERVER="imap.you.com" vendor/bin/phpunit