php-imap / php-imap
管理邮箱,在PHP中过滤/获取/删除电子邮件(支持IMAP/POP3/NNTP)
Requires
- php: ^7.4 || ^8.0
- ext-fileinfo: *
- ext-iconv: *
- ext-imap: *
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- maglnet/composer-require-checker: ^2.0|^3.2
- nikic/php-parser: ^4.3,<4.7|^4.10
- paragonie/hidden-string: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.3
- phpunit/phpunit: ^8.5|^9.5
- povils/phpmnd: ^2.2
- psalm/plugin-phpunit: ^0.10.0|^0.15.1
- roave/security-advisories: dev-master
- sebastian/phpcpd: ^4.1|^6.0
Suggests
- ext-fileinfo: To facilitate IncomingMailAttachment::getFileInfo() auto-detection
- dev-master
- 5.x-dev
- 5.0.1
- 5.0.0
- 4.x-dev
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.1.0
- 3.0.33
- 3.0.32
- 3.0.31
- 3.0.30
- 3.0.29
- 3.0.28
- 3.0.27
- 3.0.26
- 3.0.25
- 3.0.24
- 3.0.23
- 3.0.22
- 3.0.21
- 3.0.20
- 3.0.19
- 3.0.18
- 3.0.17
- 3.0.16
- 3.0.15
- 3.0.14
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.2.5
- 2.2.4
- 2.2.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0
- 1.1
- 1.0
- dev-411-OAuth-Support
- dev-Improve-php-cs-fixer-config
This package is auto-updated.
Last update: 2024-09-15 03:34:21 UTC
README
PHP IMAP 邮箱库最初于 2012 年 12 月发布,是一个强大且开源的库,可以通过 PHP IMAP 扩展连接到邮箱,支持 POP3、IMAP 和 NNTP。此库允许您从您的邮件服务器获取电子邮件。扩展功能或创建强大的 Web 应用程序来处理您的收件箱。
功能
- 通过 PHP IMAP 扩展(PHP IMAP 扩展)连接到邮箱,支持 POP3/IMAP/NNTP
- 获取带附件和内联图片的电子邮件
- 根据自定义标准过滤或排序电子邮件
- 标记电子邮件为已读/未读
- 删除电子邮件
- 管理邮箱文件夹
要求
- PHP
fileinfo
扩展必须存在;因此请确保您的 php.ini 文件中此行处于活动状态:extension=php_fileinfo.dll
- PHP
iconv
扩展必须存在;因此请确保您的 php.ini 文件中此行处于活动状态:extension=php_iconv.dll
- PHP
imap
扩展必须存在;因此请确保您的 php.ini 文件中此行处于活动状态:extension=php_imap.dll
- PHP
mbstring
扩展必须存在;因此请确保您的 php.ini 文件中此行处于活动状态:extension=php_mbstring.dll
- PHP
json
扩展必须存在;因此请确保您的 php.ini 文件中此行处于活动状态:extension=json.dll
通过 Composer 安装
安装 最新版本
$ composer require php-imap/php-imap
从 master
安装最新可用和稳定的源代码,这可能尚未发布/标记
$ composer require php-imap/php-imap:dev-master
运行测试
在您运行任何测试之前,您可能需要运行 composer install
来安装所有(开发)依赖项。
运行所有测试
您可以通过运行以下命令来运行所有可用的测试(在已安装的 php-imap
目录中): composer run tests
仅运行 PHPUnit 测试
您可以通过运行以下命令来运行所有 PHPUnit 测试(在已安装的 php-imap
目录中): php vendor/bin/phpunit --testdox
与框架的集成
- Symfony - https://github.com/secit-pl/imap-bundle
入门示例
下面,您将找到一个示例代码,说明如何使用此库。有关更多信息和其他示例,您可以查看 wiki。
默认情况下,此库使用随机文件名作为附件,因为来自其他电子邮件的相同文件名会覆盖其他附件。如果您想保留原始文件名,可以将附件文件名模式设置为 true
,但在此之后,您还需要确保这些文件不会被其他电子邮件覆盖,例如。
// Create PhpImap\Mailbox instance for all further actions $mailbox = new PhpImap\Mailbox( '{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder 'some@gmail.com', // Username for the before configured mailbox '*********', // Password for the before configured username __DIR__, // Directory, where attachments will be saved (optional) 'UTF-8', // Server encoding (optional) true, // Trim leading/ending whitespaces of IMAP path (optional) false // Attachment filename mode (optional; false = random filename; true = original filename) ); // set some connection arguments (if appropriate) $mailbox->setConnectionArgs( CL_EXPUNGE // expunge deleted mails upon mailbox close | OP_SECURE // don't do non-secure authentication ); try { // Get all emails (messages) // PHP.net imap_search criteria: https://php.ac.cn/manual/en/function.imap-search.php $mailsIds = $mailbox->searchMailbox('ALL'); } catch(PhpImap\Exceptions\ConnectionException $ex) { echo "IMAP connection failed: " . implode(",", $ex->getErrors('all')); die(); } // If $mailsIds is empty, no emails could be found if(!$mailsIds) { die('Mailbox is empty'); } // Get the first message // If '__DIR__' was defined in the first line, it will automatically // save all attachments to the specified directory $mail = $mailbox->getMail($mailsIds[0]); // Show, if $mail has one or more attachments echo "\nMail has attachments? "; if($mail->hasAttachments()) { echo "Yes\n"; } else { echo "No\n"; } // Print all information of $mail print_r($mail); // Print all attachements of $mail echo "\n\nAttachments:\n"; print_r($mail->getAttachments());
方法 imap()
允许在实例的上下文中调用任何 PHP IMAP 函数。示例
// Call imap_check() - see https://php.ac.cn/manual/function.imap-check.php $info = $mailbox->imap('check'); // Show current time for the mailbox $currentServerTime = isset($info->Date) && $info->Date ? date('Y-m-d H:i:s', strtotime($info->Date)) : 'Unknown'; echo $currentServerTime;
某些请求需要大量时间和资源
// If you don't need to grab attachments you can significantly increase performance of your application $mailbox->setAttachmentsIgnore(true); // get the list of folders/mailboxes $folders = $mailbox->getMailboxes('*'); // loop through mailboxs foreach($folders as $folder) { // switch to particular mailbox $mailbox->switchMailbox($folder['fullpath']); // search in particular mailbox $mails_ids[$folder['fullpath']] = $mailbox->searchMailbox('SINCE "1 Jan 2018" BEFORE "28 Jan 2018"'); } print_r($mails_ids);
从 3.x 升级
在 3.1 之前,Mailbox
使用一个“魔法”方法(Mailbox::imap()
),而现在的类 Imap
执行调用许多 imap_*
函数的任务,并自动进行字符串编码/解码参数和返回值
之前
public function checkMailbox() { return $this->imap('check'); }
之后
public function checkMailbox(): object { return Imap::check($this->getImapStream()); }
推荐
- Google Chrome 插件 PHP 控制台
- Google Chrome 插件 JavaScript 错误通知器