Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

php-imap

> 编程语言
Open source

Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)

1.7K stars0 likes2 views
WebsiteGitHub

About

Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)

PHP IMAP

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 (ext-imap). 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

The next major release raises the minimum supported PHP version to PHP 8.2 and is tested on PHP 8.2 through PHP 8.5.

  • PHP fileinfo, iconv, mbstring, and json extensions must be present.
  • PHP ext-imap must be present.
  • On PHP 8.2 and 8.3, install or enable the IMAP extension provided by your PHP distribution. On Windows, this can still mean enabling extension=php_imap.dll in php.ini.
  • On PHP 8.4 and newer, install IMAP from PECL and enable it for your CLI and web SAPIs.
  • When building IMAP from source, you may also need c-client, OpenSSL, and Kerberos development libraries.
  • ext-imap is not thread-safe and should not be used with ZTS builds.

Installation by Composer

Before running composer require or composer install, make sure ext-imap is installed for the PHP version you are using.

Install the latest available release:

$ composer require php-imap/php-imap

Install the latest available and stable source code from master, which is may not released / tagged yet:

$ composer require php-imap/php-imap:dev-master

Run Tests

Before you can run any tests you need a working ext-imap installation and you may need to run composer install to install all development dependencies.

Run all tests

You can run all available tests by running the following command (inside of the installed php-imap directory): composer run tests

Run only PHPUnit tests

You can run all PHPUnit tests by running the following command (inside of the installed php-imap directory): php vendor/bin/phpunit --testdox

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. For backward compatibility, this still overwrites an existing file with the same name. If you want to keep the original file name and automatically suffix duplicates with (1), (2), and so on, set the attachment filename collision mode to PhpImap\Mailbox::ATTACHMENT_FILENAME_COLLISION_SUFFIX.

…

searchMailbox() delegates criteria evaluation to PHP's IMAP extension and the IMAP server behind it. This library includes a fallback for simple SEEN ... SINCE ... searches because some ext-imap / server combinations do not immediately return messages marked as seen earlier on the same day. More complex imap_search() behavior still depends on the underlying IMAP implementation.

Method imap() allows to call any PHP IMAP function in a context of the instance. Example:

php
// Call imap_check() - see http://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:

php
// 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);

Upgrading from 3.x

Prior to 3.1, Mailbox used a "magic" method (Mailbox::imap()), with the class Imap now performing it's purpose to call many imap_* functions with automated string encoding/decoding of arguments and return values:

Before:

php
    public function checkMailbox()
    {
        return $this->imap('check');
    }

After:

php
    public function checkMailbox(): object
    {
        return Imap::check($this->getImapStream());
    }

Recommended

  • Google Chrome extension PHP Console
  • Google Chrome extension JavaScript Errors Notifier

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言