2017 © Pedro Peláez
 

library imap

Object-oriented IMAP for PHP

image

ddeboer/imap

Object-oriented IMAP for PHP

  • Friday, July 20, 2018
  • by ddeboer
  • Repository
  • 28 Watchers
  • 406 Stars
  • 200,273 Installations
  • PHP
  • 8 Dependents
  • 0 Suggesters
  • 187 Forks
  • 24 Open issues
  • 36 Versions
  • 15 % Grown

The README.md

PHP IMAP library

Latest Stable Version Downloads Integrate Code Coverage, (*1)

A PHP IMAP library to read and process e-mails over IMAP protocol, built with robust Object-Oriented architecture., (*2)

This library requires PHP >= 8.3 with IMAP, iconv and Multibyte String extensions installed., (*3)

Installation

The recommended way to install the IMAP library is through Composer:, (*4)

$ composer require ddeboer/imap

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*5)

Usage

Connect and Authenticate

use Ddeboer\Imap\Server;

$server = new Server('imap.gmail.com');

// $connection is instance of \Ddeboer\Imap\Connection
$connection = $server->authenticate('my_username', 'my_password');

You can specify port, flags and parameters to the server:, (*6)

$server = new Server(
    $hostname, // required
    $port,     // defaults to '993'
    $flags,    // defaults to '/imap/ssl/validate-cert'
    $parameters
);

Mailboxes

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

$mailboxes = $connection->getMailboxes();

foreach ($mailboxes as $mailbox) {
    // Skip container-only mailboxes
    // @see https://secure.php.net/manual/en/function.imap-getmailboxes.php
    if ($mailbox->getAttributes() & \LATT_NOSELECT) {
        continue;
    }

    // $mailbox is instance of \Ddeboer\Imap\Mailbox
    printf('Mailbox "%s" has %s messages', $mailbox->getName(), $mailbox->count());
}

Or retrieve a specific mailbox:, (*8)

$mailbox = $connection->getMailbox('INBOX');

Delete a mailbox:, (*9)

$connection->deleteMailbox($mailbox);

You can bulk set, or clear, any flag of mailbox messages (by UIDs):, (*10)

$mailbox->setFlag('\\Seen \\Flagged', ['1:5', '7', '9']);
$mailbox->setFlag('\\Seen', '1,3,5,6:8');

$mailbox->clearFlag('\\Flagged', '1,3');

WARNING You must retrieve new Message instances in case of bulk modify flags to refresh the single Messages flags., (*11)

Messages

Retrieve messages (e-mails) from a mailbox and iterate over them:, (*12)

$messages = $mailbox->getMessages();

foreach ($messages as $message) {
    // $message is instance of \Ddeboer\Imap\Message
}

To insert a new message (that just has been sent) into the Sent mailbox and flag it as seen:, (*13)

$mailbox = $connection->getMailbox('Sent');
$mailbox->addMessage($messageMIME, '\\Seen');

Note that the message should be a string at MIME format (as described in the RFC2045)., (*14)

Searching for Messages

use Ddeboer\Imap\SearchExpression;
use Ddeboer\Imap\Search\Email\To;
use Ddeboer\Imap\Search\Text\Body;

$search = new SearchExpression();
$search->addCondition(new To('me@here.com'));
$search->addCondition(new Body('contents'));

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

WARNING We are currently unable to have both spaces and double-quotes escaped together. Only spaces are currently escaped correctly. You can use Ddeboer\Imap\Search\RawExpression to write the complete search condition by yourself., (*15)

Messages can also be retrieved sorted as per imap_sort function:, (*16)

$today = new DateTimeImmutable();
$thirtyDaysAgo = $today->sub(new DateInterval('P30D'));

$messages = $mailbox->getMessages(
    new Ddeboer\Imap\Search\Date\Since($thirtyDaysAgo),
    \SORTDATE, // Sort criteria
    true // Descending order
);

Unknown search criterion: OR

Note that PHP imap library relies on the c-client library available at https://www.washington.edu/imap/ which doesn't fully support some IMAP4 search criteria like OR. If you want those unsupported criteria, you need to manually patch the latest version (imap-2007f of 23-Jul-2011 at the time of this commit) and recompile PHP onto your patched c-client library., (*17)

By the way most of the common search criteria are available and functioning, browse them in ./src/Search., (*18)

References:, (*19)

  1. https://stackoverflow.com/questions/36356715/imap-search-unknown-search-criterion-or
  2. imap-2007f.tar.gz: ./src/c-client/mail.c and ./docs/internal.txt

Message Properties and Operations

Get message number and unique message id in the form ...:, (*20)

$message->getNumber();
$message->getId();

Get other message properties:, (*21)

$message->getSubject();
$message->getFrom();    // Message\EmailAddress
$message->getTo();      // array of Message\EmailAddress
$message->getDate();    // DateTimeImmutable
$message->isAnswered();
$message->isDeleted();
$message->isDraft();
$message->isSeen();

Get message headers as a \Ddeboer\Imap\Message\Headers object:, (*22)

$message->getHeaders();

Get message body as HTML or plain text (only first part):, (*23)

$message->getBodyHtml();    // Content of text/html part, if present
$message->getBodyText();    // Content of text/plain part, if present

Get complete body (all parts):, (*24)

$body = $message->getCompleteBodyHtml();    // Content of text/html part, if present
if ($body === null) { // If body is null, there are no HTML parts, so let's try getting the text body
    $body = $message->getCompleteBodyText();    // Content of text/plain part, if present
}

Reading the message body keeps the message as unseen. If you want to mark the message as seen:, (*25)

$message->markAsSeen();

Or you can set, or clear, any flag:, (*26)

$message->setFlag('\\Seen \\Flagged');
$message->clearFlag('\\Flagged');

Move a message to another mailbox:, (*27)

$mailbox = $connection->getMailbox('another-mailbox');
$message->move($mailbox);

Deleting messages:, (*28)

$mailbox->getMessage(1)->delete();
$mailbox->getMessage(2)->delete();
$connection->expunge();

Message Attachments

Get message attachments (both inline and attached) and iterate over them:, (*29)

$attachments = $message->getAttachments();

foreach ($attachments as $attachment) {
    // $attachment is instance of \Ddeboer\Imap\Message\Attachment
}

Download a message attachment to a local file:, (*30)

// getDecodedContent() decodes the attachment’s contents automatically:
file_put_contents(
    '/my/local/dir/' . $attachment->getFilename(),
    $attachment->getDecodedContent()
);

Embedded Messages

Check if attachment is embedded message and get it:, (*31)

$attachments = $message->getAttachments();

foreach ($attachments as $attachment) {
    if ($attachment->isEmbeddedMessage()) {
        $embeddedMessage = $attachment->getEmbeddedMessage();
        // $embeddedMessage is instance of \Ddeboer\Imap\Message\EmbeddedMessage
    }
}

An EmbeddedMessage has the same API as a normal Message, apart from flags and operations like copy, move or delete., (*32)

Timeouts

The IMAP extension provides the imap_timeout function to adjust the timeout seconds for various operations., (*33)

However the extension's implementation doesn't link the functionality to a specific context or connection, instead they are global. So in order to not affect functionalities outside this library, we had to choose whether wrap every imap_* call around an optional user-provided timeout or leave this task to the user., (*34)

Because of the heterogeneous world of IMAP servers and the high complexity burden cost for such a little gain of the former, we chose the latter., (*35)

Mock the library

Mockability is granted by interfaces present for each API. Dig into MockabilityTest for an example of a mocked workflow., (*36)

Contributing: run the build locally

Docker is needed to run the build on your computer., (*37)

First command you need to run is make start-imap-server, which starts an IMAP server locally., (*38)

Then the local build can be triggered with a bare make., (*39)

When you finish the development, stop the local IMAP server with make stop-imap-server., (*40)

The Versions

20/07 2018

dev-master

9999999-dev

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • ext-imap *
  • php ^7.0
  • ext-mbstring *
  • ext-iconv *

 

The Development Requires

by Filippo Tessarotto

mail email imap

20/07 2018

1.5.3

1.5.3.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

10/07 2018

1.5.2

1.5.2.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

04/05 2018

1.5.1

1.5.1.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

26/03 2018

1.5.0

1.5.0.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

22/03 2018

1.4.1

1.4.1.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

19/03 2018

1.4.0

1.4.0.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

09/03 2018

1.3.1

1.3.1.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

28/02 2018

1.3.0

1.3.0.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

09/02 2018

1.2.3

1.2.3.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

05/02 2018

1.2.2

1.2.2.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

29/01 2018

1.2.1

1.2.1.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

15/01 2018

1.2.0

1.2.0.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

12/12 2017

1.1.2

1.1.2.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

10/11 2017

1.1.1

1.1.1.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

06/11 2017

1.1.0

1.1.0.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-iconv *
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

27/10 2017

v1.0.x-dev

1.0.9999999.9999999-dev

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

27/10 2017

1.0.8

1.0.8.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

16/10 2017

1.0.7

1.0.7.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

12/10 2017

1.0.6

1.0.6.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

12/10 2017

1.0.5

1.0.5.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

11/10 2017

1.0.4

1.0.4.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

11/10 2017

1.0.3

1.0.3.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

06/10 2017

1.0.2

1.0.2.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

05/10 2017

1.0.1

1.0.1.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

04/10 2017

1.0.0

1.0.0.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-imap *
  • ext-mbstring *

 

The Development Requires

by Filippo Tessarotto

mail email imap

28/09 2017

dev-develop

dev-develop

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

  • ext-imap *
  • php ^7.0
  • ext-mbstring *

 

The Development Requires

mail email imap

06/05 2016

dev-travis-hhvm-php7

dev-travis-hhvm-php7

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

mail email imap

03/12 2015

0.5.2

0.5.2.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

mail email imap

01/02 2015

0.5.1

0.5.1.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

mail email imap

24/01 2015

0.5.0

0.5.0.0

Object-oriented IMAP for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

mail email imap

04/01 2015

0.4.0

0.4.0.0

Object-oriented IMAP library for PHP 5.3+

  Sources   Download

MIT

The Requires

 

The Development Requires

11/08 2014

0.3.1

0.3.1.0

Receive and send e-mails over IMAP

  Sources   Download

MIT

The Requires

 

The Development Requires

10/08 2014

0.3.0

0.3.0.0

Receive and send e-mails over IMAP

  Sources   Download

MIT

The Requires

 

The Development Requires

24/11 2013

0.2

0.2.0.0

Receive and send e-mails over IMAP

  Sources   Download

MIT

The Requires

 

The Development Requires

23/11 2013

0.1

0.1.0.0

Receive and send e-mails over IMAP

  Sources   Download

MIT

The Requires

 

The Development Requires