evoweb/php-imap
Composer 安装命令:
composer require evoweb/php-imap
包简介
Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
README 文档
README
Based on works until December 2022 in barbushin/php-imap with merged pull requests
Initially released in December 2012, the PHP IMAP Mailbox is a powerful and open source library to connect to a mailbox by POP3, IMAP and NNTP using the PHP IMAP extension. This library allows you to fetch emails from your email server. Extend the functionality or create powerful web applications to handle your incoming emails.
Features
- Connect to mailbox by POP3/IMAP/NNTP, using PHP IMAP extension
- Get emails with attachments and inline images
- Get emails filtered or sorted by custom criteria
- Mark emails as seen/unseen
- Delete emails
- Manage mailbox folders
Requirements
| PHP Version | php-imap Version | php-imap status |
|---|---|---|
| 5.6 | 3.x | End of life |
| 7.0 | 3.x | End of life |
| 7.1 | 3.x | End of life |
| 7.2 | 3.x, 4.x | End of life |
| 7.3 | 3.x, 4.x | End of life |
| 7.4 | >3.0.33, 4.x, 5.x | End of life |
| 8.0 | >3.0.33, 4.x, 5.x | End of life |
| 8.1 | >4.3.0, 5.x | End of life |
| 8.2 | 6.x | Active support |
| 8.3 | 6.x | Active support |
| 8.4 | 6.x | Active support |
| 8.5 | 6.x | Active support |
- PHP
fileinfoextension must be present; so make sure this line is active in your php.ini:extension=php_fileinfo.dll - PHP
iconvextension must be present; so make sure this line is active in your php.ini:extension=php_iconv.dll - PHP
imapextension must be present; so make sure this line is active in your php.ini:extension=php_imap.dll - PHP
mbstringextension must be present; so make sure this line is active in your php.ini:extension=php_mbstring.dll - PHP
jsonextension must be present; so make sure this line is active in your php.ini:extension=json.dll
Installation by Composer
Install the latest available release:
$ composer require evoWeb/php-imap
Install the latest available and stable source code from develop, which may not be released / tagged yet:
$ composer require evoWeb/php-imap:dev-develop
Development
This package supports the development with an ddev configuration. That requires to have docker and ddev installed. In addition the make tool is highly suggested.
Run Tests
Before you can run any tests you may need to run make install or ddev composer install to install all (development) dependencies.
Run all tests
You can run all available tests by running the following command (inside the installed php-imap directory): make test or ddev composer run tests
Run only PHPUnit tests
You can run all PHPUnit tests by running the following command (inside the installed php-imap directory): make phpunit or ddev composer run phpunit
Integration with frameworks
- Symfony - https://github.com/secit-pl/imap-bundle
Getting Started Example
Below, you'll find an example code how you can use this library. For further information and other examples, you may take a look at the wiki.
By default, this library uses random filenames for attachments as identical file names from other emails would overwrite other attachments. If you want to keep the original file name, you can set the attachment filename mode to true, but then you also need to ensure, that those files don't get overwritten by other emails for example.
// 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.net/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 attachments of $mail echo "\n\nAttachments:\n"; print_r($mail->getAttachments());
Method imap() allows to call any PHP IMAP function in a context of the instance. Example:
// Call imap_check() - see https://php.net/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;
Some request require much time and resources:
// 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 mailboxes 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);
Upgrading from below 6.x
BREAKING: Change handling of data. For clearer typing the following entities were introduced:
- ComposeBody
- ComposeEnvelope
- HostnameAndAddress
- MailOverview
- PartStructure
- PartStructureParameter
Before replacing any array with these, the tests where improved to cover them all.
BREAKING: Before each method in Imap checked if it really got a connection handed. Now the functions are enforcing connection with typed arguments. You are still able to use Imap::EnsureConnection() yourself if you need to check your argument.
Functions are convertest to camel case:
-
Imap::createmailbox -> Imap::createMailbox (public)
-
Imap::deletemailbox -> Imap::deleteMailbox (public)
-
Imap::EnsureConnection -> Imap::ensureConnection (public)
-
Imap::EnsureRange -> Imap::ensureRange (private)
-
Imap::EnsureResource -> Imap::ensureResource (private)
-
Imap::fetchbody -> Imap::fetchBody (public)
-
Imap::fetchheader -> Imap::fetchHeader (public)
-
Imap::fetchstructure -> Imap::fetchStructure (public)
-
Imap::getmailboxes -> Imap::getMailboxes (public)
-
Imap::getsubscribed -> Imap::getSubscribed (public)
-
Imap::fetch_overview -> Imap::fetchOverview (public)
-
Imap::get_quotaroot -> Imap::getQuotaRoot (public)
-
Imap::HandleErrors -> Imap::handleErrors (private)
-
Imap::mail_compose -> Imap::mailCompose (public)
-
Imap::mail_copy -> Imap::mailCopy (public)
-
Imap::mail_move -> Imap::mailMove (public)
-
Imap::mailboxmsginfo -> Imap::mailboxMsgInfo (public)
-
Imap::num_msg -> Imap::numMsg (public)
-
Imap::renamemailbox -> Imap::renameMailbox (public)
-
Imap::savebody -> Imap::saveBody (public)
-
Imap::clearflag_full -> Imap::clearFlagFull (public)
-
Imap::setflag_full -> Imap::setFlagFull (public)
-
Mailbox::lowercase_mb_list_encodings -> Mailbox::lowercaseMbListEncodings (protected)
Private property IncomingMailAttachment::file_path replaced with IncomingMailAttachment::filePath.
Upgrading from 3.x
Prior to 3.1, Mailbox used a "magic" method (Mailbox::imap()), with the
class Imap now performing its purpose to call many imap_* functions with
automated string encoding/decoding of arguments and return values:
Before:
public function checkMailbox() { return $this->imap('check'); }
After:
public function checkMailbox(): object { return Imap::check($this->getImapStream()); }
evoweb/php-imap 适用场景与选型建议
evoweb/php-imap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mail」 「php」 「imap」 「pop3」 「mailbox」 「receive emails」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 evoweb/php-imap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 evoweb/php-imap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 evoweb/php-imap 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP class to access an IMAP mailbox. Specifically uses the Laminas library and not the IMAP extension.
yii2 extension to read and process mails from IMAP and PHP
IMAP client for PHP without php-imap extension dependency
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
Mail libraries used by Zimbra Api
Laravel IMAP client
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-05