You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Go to file
Simon Vieille aef0637949
update composer
10 months ago
examples add skelton 6 years ago
src fix notice "Notice: Undefined property: stdClass::$dparameters" 10 months ago
tests enhance code style issues and create Makefile 4 years ago
phpunit.xml.dist enhance code style issues and create Makefile 4 years ago
.gitignore enhance code style issues and create Makefile 4 years ago
.php_cs.dist enhance code style issues and create Makefile 4 years ago
.travis.yml Update .travis.yml 3 years ago Create 4 years ago Create 4 years ago
LICENSE Fix the thing 3 years ago
Makefile enhance code style issues and create Makefile 4 years ago Fix the thing 3 years ago
composer.json update composer 10 months ago
composer.lock bump pkgs 3 years ago
phpcs.xml Update phpcs.xml 4 years ago
phpunit.xml.dist add skelton 6 years ago
renovate.json Add renovate.json 3 years ago


📬 Access Mailbox Using PHP IMAP.

Current Version: 1.0.6

Build Status


To install the package via composer, use the following:

composer require clivern/imap

This command requires you to have Composer installed globally.


After adding the package as a dependency, Please read the following steps:

Connect and Authenticate

include_once dirname(__FILE__) . '/vendor/autoload.php';

use Clivern\Imap\Core\Connection;

$connection = new Connection(

After end of everything, you should close connection


Connection Options

$folder = "INBOX";

// Reconnect & Update Mailbox Folder
// Reopen Connection
// Get Connection Stream
// Get Server String
// Check Connection
// Get Quota Array
// Get Status Array
// Check MailBox Data
// Ping Connection
// Get Errors
// Get Alerts
// Get Last Error
// Disconnect


Retrieve mailboxes (also known as mail folders) from the mail server and iterate over them:

use Clivern\Imap\MailBox;

$mailbox = new MailBox($connection);

$messages = $mailbox->getMessages();

foreach ($messages as $message) {
    echo "Subject: " . $message->header()->get('subject');
    echo "<br/>";
    echo $message->body()->getMessage();


To add custom search

use Clivern\Imap\Core\Search;
use Clivern\Imap\Core\Search\Condition\All;
use Clivern\Imap\Core\Search\Condition\Answered;
use Clivern\Imap\Core\Search\Condition\BCC;
use Clivern\Imap\Core\Search\Condition\Before;
use Clivern\Imap\Core\Search\Condition\Body;
use Clivern\Imap\Core\Search\Condition\CC;
use Clivern\Imap\Core\Search\Condition\Deleted;
use Clivern\Imap\Core\Search\Condition\Flagged;
use Clivern\Imap\Core\Search\Condition\From;
use Clivern\Imap\Core\Search\Condition\Keyword;
use Clivern\Imap\Core\Search\Condition\NewFlag;
use Clivern\Imap\Core\Search\Condition\Old;
use Clivern\Imap\Core\Search\Condition\On;
use Clivern\Imap\Core\Search\Condition\Recent;
use Clivern\Imap\Core\Search\Condition\Seen;
use Clivern\Imap\Core\Search\Condition\Since;
use Clivern\Imap\Core\Search\Condition\Subject;
use Clivern\Imap\Core\Search\Condition\Text;
use Clivern\Imap\Core\Search\Condition\To;
use Clivern\Imap\Core\Search\Condition\UnAnswered;
use Clivern\Imap\Core\Search\Condition\UnDeleted;
use Clivern\Imap\Core\Search\Condition\UnFlagged;
use Clivern\Imap\Core\Search\Condition\UnKeyword;
use Clivern\Imap\Core\Search\Condition\UnSeen;

$search = new Search();
$search->addCondition(new All());
// $search->addCondition(new Answered());
// $search->addCondition(new BCC(""));
// $search->addCondition(new Before(date("j F Y")));
// $search->addCondition(new Body("search text"));
// $search->addCondition(new CC(""));
// $search->addCondition(new Deleted());
// $search->addCondition(new Flagged());
// $search->addCondition(new From(""));
// $search->addCondition(new Keyword("test"));
// $search->addCondition(new NewFlag());
// $search->addCondition(new Old());
// $search->addCondition(new On(date("j F Y")));
// $search->addCondition(new Recent());
// $search->addCondition(new Seen());
// $search->addCondition(new Since(date("j F Y")));
// $search->addCondition(new Subject("search text"));
// $search->addCondition(new Text("search text"));
// $search->addCondition(new To(""));
// $search->addCondition(new UnAnswered());
// $search->addCondition(new UnDeleted());
// $search->addCondition(new UnFlagged());
// $search->addCondition(new UnKeyword("test"));
// $search->addCondition(new UnSeen());

// For more info, please check

Then configure mailbox:

use Clivern\Imap\MailBox;

$mailbox = new MailBox($connection);

$messages = $mailbox->getMessages($search);

foreach ($messages as $message) {
    echo "Subject: " . $message->header()->get('subject');
    echo "<br/>";
    echo $message->body()->getMessage();

Mailbox Option

Some good methods in mailbox

use Clivern\Imap\MailBox;

$mailbox = new MailBox($connection);
// Get Folders
// Update Folder
$mailbox->setFolder("[Gmail]/All Mail");
// Count Messages in Current Folder

$messages = $mailbox->getMessages();

foreach ($messages as $message) {
    echo "Subject: " . $message->header()->get('subject');
    echo "<br/>";
    echo $message->body()->getMessage();


To get message header data:


To get message body


To get message attachments

$attachments = $message->attachments();
foreach ($attachments as $attachment) {
    // get attachment content
    // get decoded attachment content
    // Store attachment in provided path
    $attachment->store(__DIR__ . '/');

To do actions on message like delete or undelete

// and don't forget to run the following to delete all messages marked for deletion



Version 1.0.6:

Fix Class Name.

Version 1.0.5:

Enhance code style.
Automate code fixes and linting.

Version 1.0.4:

Fix for plain text messages.

Version 1.0.3:

Fix Attachment Object.

Version 1.0.2:

Message delete & undelete actions added.

Version 1.0.1:

Debug data removed.

Version 1.0.0:

Initial Release.


© 2019, Clivern. Released under the MIT License.

Imap is authored and maintained by @clivern.