PHPackages                             php-imap/php-imap - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. php-imap/php-imap

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

php-imap/php-imap
=================

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

6.0.0(1mo ago)1.7k13.4M↓38.9%458[49 issues](https://github.com/barbushin/php-imap/issues)[1 PRs](https://github.com/barbushin/php-imap/pulls)20MITPHPPHP ^8.2CI failing

Since Apr 20Pushed 1mo ago67 watchersCompare

[ Source](https://github.com/barbushin/php-imap)[ Packagist](https://packagist.org/packages/php-imap/php-imap)[ Docs](https://github.com/barbushin/php-imap)[ RSS](/packages/php-imap-php-imap/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (20)Versions (91)Used By (20)

PHP IMAP
========

[](#php-imap)

[![GitHub release](https://camo.githubusercontent.com/dba4505d51134c130aa4efdf9f4443efb8d74630ebc17b011e491ef7d3937201/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f62617262757368696e2f7068702d696d61702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/php-imap/php-imap)[![Supported PHP Version](https://camo.githubusercontent.com/8742f8e04abb9f6509c145e429d373f1a5317c1a993707fae23f6a0004b4684d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7068702d696d61702f7068702d696d61702e737667)](README.md)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist](https://camo.githubusercontent.com/e02c7304601b7b149a2fca09d0d6f30502710a288b844cac1ce298f5b7c0a883/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7068702d696d61702f7068702d696d61702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/php-imap/php-imap)

[![CI PHP Unit Tests](https://github.com/barbushin/php-imap/actions/workflows/php_unit_tests.yml/badge.svg?branch=master)](https://github.com/barbushin/php-imap/actions/workflows/php_unit_tests.yml)[![CI PHP Static Analysis](https://github.com/barbushin/php-imap/actions/workflows/php_static_analysis.yml/badge.svg?branch=master)](https://github.com/barbushin/php-imap/actions/workflows/php_static_analysis.yml)[![CI PHP Code Coverage](https://github.com/barbushin/php-imap/actions/workflows/php_code_coverage.yml/badge.svg?branch=master)](https://github.com/barbushin/php-imap/actions/workflows/php_code_coverage.yml)

[![Maintainability](https://camo.githubusercontent.com/95f21b236e21ab56acc516bd58422863bbccda6c15e83795dfe89b32f986956b/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f30326637326134666436393563623765323937362f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/barbushin/php-imap/maintainability)[![Test Coverage](https://camo.githubusercontent.com/735583e18168758176aa53899e235a249acabb9d11cbd72a026eee4979ba660e/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f30326637326134666436393563623765323937362f746573745f636f766572616765)](https://codeclimate.com/github/barbushin/php-imap/test_coverage)[![Type Coverage](https://camo.githubusercontent.com/5c485249ba36c9a51a5a6ec3b709317e22f3d84b53eae0af8f8ec4eb8508a405/68747470733a2f2f73686570686572642e6465762f6769746875622f62617262757368696e2f7068702d696d61702f636f7665726167652e737667)](https://shepherd.dev/github/barbushin/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

[](#features)

- Connect to mailbox by POP3/IMAP/NNTP, using [PHP IMAP extension](https://www.php.net/manual/book.imap.php)
- 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

[](#requirements)

PHP Versionphp-imap Versionphp-imap status5.63.xEnd of life7.03.xEnd of life7.13.xEnd of life7.23.x, 4.xEnd of life7.33.x, 4.xEnd of life7.4&gt;3.0.33, 4.x, 5.xEnd of life8.0&gt;3.0.33, 4.x, 5.xEnd of life8.1&gt;4.3.0, 5.xEnd of life8.26.xActive support8.36.xActive support8.46.xActive support8.56.xActive supportThe 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

[](#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](https://github.com/barbushin/php-imap/releases):

```
$ 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

[](#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

[](#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

[](#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

[](#integration-with-frameworks)

- Symfony -

### Getting Started Example

[](#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](https://github.com/barbushin/php-imap/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`.

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

$mailbox->setAttachmentFilenameCollisionMode(
    PhpImap\Mailbox::ATTACHMENT_FILENAME_COLLISION_SUFFIX
);

// set some connection arguments (if appropriate)
$mailbox->setConnectionArgs(
    CL_EXPUNGE // expunge deleted mails upon mailbox close
    | OP_SECURE // don't do non-secure authentication
);

// Some providers require OAuth instead of a password.
// Obtain and refresh the access token outside of this library, then enable OAuth explicitly.
// Your ext-imap build must expose OP_XOAUTH2 for this to work.
$mailbox->enableOAuth($accessToken);

try {
	// Get all emails (messages)
	// PHP.net imap_search criteria: http://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');
}

// If you want to inspect a message without changing its seen state,
// use getMail($id, false) or getRawMail($id, false).

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

// Access arbitrary headers without adding custom properties to the library
$originMessageId = $mail->getHeader('Origin-MessageID');
$receivedHeaders = $mail->getHeaders('Received');

// Print all attachements of $mail
echo "\n\nAttachments:\n";
print_r($mail->getAttachments());
```

`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](https://www.php.net/manual/ref.imap.php) in a context of the instance. Example:

```
// 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:

```
// 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

[](#upgrading-from-3x)

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:

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

After:

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

### Recommended

[](#recommended)

- Google Chrome extension [PHP Console](https://chrome.google.com/webstore/detail/php-console/nfhmhhlpfleoednkpnnnkolmclajemef)
- Google Chrome extension [JavaScript Errors Notifier](https://chrome.google.com/webstore/detail/javascript-errors-notifie/jafmfknfnkoekkdocjiaipcnmkklaajd)

###  Health Score

79

—

ExcellentBetter than 100% of packages

Maintenance88

Actively maintained with recent releases

Popularity74

Solid adoption and visibility

Community50

Growing community involvement

Maturity91

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~58 days

Recently: every ~313 days

Total

70

Last Release

54d ago

Major Versions

1.1 → 2.02015-04-20

2.2.5 → 3.0.02017-10-06

3.1.0 → 4.0.02020-04-22

4.x-dev → 5.0.02022-03-12

5.x-dev → 6.0.02026-05-10

PHP version history (8 changes)1.0PHP &gt;=5.3.0

3.0.0PHP &gt;=5.5

3.0.9PHP &gt;=5.6

4.0.0PHP ^7.2

4.2.0PHP ^7.2 || ^8.0 &lt;8.1

4.3.0PHP ^7.2 || ^8.0

5.0.0PHP ^7.4 || ^8.0

6.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/683352?v=4)[Sergey](/maintainers/barbushin)[@barbushin](https://github.com/barbushin)

---

Top Contributors

[![bapcltd-marv](https://avatars.githubusercontent.com/u/46891309?v=4)](https://github.com/bapcltd-marv "bapcltd-marv (387 commits)")[![Sebbo94BY](https://avatars.githubusercontent.com/u/5154682?v=4)](https://github.com/Sebbo94BY "Sebbo94BY (229 commits)")[![barbushin](https://avatars.githubusercontent.com/u/683352?v=4)](https://github.com/barbushin "barbushin (148 commits)")[![quentinus95](https://avatars.githubusercontent.com/u/8060564?v=4)](https://github.com/quentinus95 "quentinus95 (13 commits)")[![ThomasLandauer](https://avatars.githubusercontent.com/u/1054469?v=4)](https://github.com/ThomasLandauer "ThomasLandauer (12 commits)")[![SRWieZ](https://avatars.githubusercontent.com/u/1408020?v=4)](https://github.com/SRWieZ "SRWieZ (8 commits)")[![twmobius](https://avatars.githubusercontent.com/u/854155?v=4)](https://github.com/twmobius "twmobius (6 commits)")[![stanma](https://avatars.githubusercontent.com/u/1821782?v=4)](https://github.com/stanma "stanma (5 commits)")[![JellyBellyDev](https://avatars.githubusercontent.com/u/190820?v=4)](https://github.com/JellyBellyDev "JellyBellyDev (4 commits)")[![commanddotcom](https://avatars.githubusercontent.com/u/735385?v=4)](https://github.com/commanddotcom "commanddotcom (4 commits)")[![sharevb](https://avatars.githubusercontent.com/u/29117330?v=4)](https://github.com/sharevb "sharevb (3 commits)")[![dizzy7](https://avatars.githubusercontent.com/u/1052472?v=4)](https://github.com/dizzy7 "dizzy7 (3 commits)")[![underdpt](https://avatars.githubusercontent.com/u/8122137?v=4)](https://github.com/underdpt "underdpt (2 commits)")[![aragon999](https://avatars.githubusercontent.com/u/6317761?v=4)](https://github.com/aragon999 "aragon999 (2 commits)")[![ebrana-devs](https://avatars.githubusercontent.com/u/7850167?v=4)](https://github.com/ebrana-devs "ebrana-devs (2 commits)")[![fmayorov](https://avatars.githubusercontent.com/u/120632579?v=4)](https://github.com/fmayorov "fmayorov (2 commits)")[![georaldc](https://avatars.githubusercontent.com/u/286812?v=4)](https://github.com/georaldc "georaldc (2 commits)")[![hyncica](https://avatars.githubusercontent.com/u/11272692?v=4)](https://github.com/hyncica "hyncica (2 commits)")[![janbarasek](https://avatars.githubusercontent.com/u/4738758?v=4)](https://github.com/janbarasek "janbarasek (2 commits)")[![Jekis](https://avatars.githubusercontent.com/u/1102024?v=4)](https://github.com/Jekis "Jekis (2 commits)")

---

Tags

phpmailimappop3mailboxreceive emails

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/php-imap-php-imap/health.svg)

```
[![Health](https://phpackages.com/badges/php-imap-php-imap/health.svg)](https://phpackages.com/packages/php-imap-php-imap)
```

###  Alternatives

[webklex/php-imap

PHP IMAP client

4466.7M27](/packages/webklex-php-imap)[benhall14/php-imap-reader

A PHP class that makes working with IMAP in PHP simple.

3519.6k](/packages/benhall14-php-imap-reader)[henrique-borba/php-sieve-manager

A modern (started in 2022) PHP library for the ManageSieve protocol (RFC5804) to create/edit Sieve scripts (RFC5228). Used by Cypht Webmail.

28142.6k4](/packages/henrique-borba-php-sieve-manager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
