2017 © Pedro Peláez
 

library php-imap

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

image

php-imap/php-imap

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

  • Saturday, April 14, 2018
  • by barbushin
  • Repository
  • 71 Watchers
  • 855 Stars
  • 585,398 Installations
  • PHP
  • 30 Dependents
  • 0 Suggesters
  • 294 Forks
  • 53 Open issues
  • 26 Versions
  • 11 % Grown

The README.md

PHP IMAP

GitHub release Supported PHP Version Software License Packagist, (*1)

CI PHP Unit Tests CI PHP Static Analysis CI PHP Code Coverage, (*2)

Maintainability Test Coverage Type Coverage, (*3)

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., (*4)

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 Active support
8.0 >3.0.33, 4.x, 5.x Active support
8.1 >4.3.0, 5.x Active support
  • PHP fileinfo extension must be present; so make sure this line is active in your php.ini: extension=php_fileinfo.dll
  • PHP iconv extension must be present; so make sure this line is active in your php.ini: extension=php_iconv.dll
  • PHP imap extension must be present; so make sure this line is active in your php.ini: extension=php_imap.dll
  • PHP mbstring extension must be present; so make sure this line is active in your php.ini: extension=php_mbstring.dll
  • PHP json extension 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:, (*5)

$ composer require php-imap/php-imap

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

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

Run Tests

Before you can run the any tests you may need to run composer install to install all (development) dependencies., (*7)

Run all tests

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

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, (*9)

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., (*10)

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., (*11)

// 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: 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');
}

// 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 attachements 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:, (*12)

// 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:, (*13)

// 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:, (*14)

Before:, (*15)

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

After:, (*16)

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

The Versions

14/04 2018

dev-master

9999999-dev https://github.com/barbushin/php-imap

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

  Sources   Download

MIT BSD 3-Clause

The Requires

  • ext-imap *
  • php >=5.5

 

mail php imap pop3 mailbox receive emails

22/12 2017

3.0.6

3.0.6.0 https://github.com/barbushin/php-imap

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-imap *

 

mail php imap pop3 mailbox receive emails

30/11 2017

3.0.5

3.0.5.0 https://github.com/barbushin/php-imap

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-imap *

 

mail php imap pop3 mailbox receive emails

15/11 2017

3.0.4

3.0.4.0 https://github.com/barbushin/php-imap

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-imap *

 

mail php imap pop3 mailbox receive emails

11/11 2017

3.0.3

3.0.3.0 https://github.com/barbushin/php-imap

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-imap *

 

mail php imap pop3 mailbox receive emails

10/11 2017

3.0.2

3.0.2.0 https://github.com/barbushin/php-imap

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-imap *

 

mail php imap pop3 mailbox receive emails

20/10 2017

3.0.1

3.0.1.0 https://github.com/barbushin/php-imap

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-imap *

 

mail php imap pop3 mailbox receive emails

06/10 2017

3.0.0

3.0.0.0 https://github.com/barbushin/php-imap

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

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-imap *

 

mail php imap pop3 mailbox receive emails

06/10 2017

2.2.5

2.2.5.0 https://github.com/barbushin/php-imap

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap pop3 mailbox receive emails

06/10 2017

2.2.4

2.2.4.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

06/10 2017

2.2.3

2.2.3.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

29/08 2017

2.1.2

2.1.2.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

23/08 2017

2.1.1

2.1.1.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

23/08 2017

2.1.0

2.1.0.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

23/08 2017

2.0.9

2.0.9.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

13/08 2016

2.0.8

2.0.8.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

18/07 2016

2.0.7

2.0.7.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

14/12 2015

2.0.6

2.0.6.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

11/12 2015

2.0.5

2.0.5.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

06/11 2015

2.0.4

2.0.4.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0
  • ext-imap *

 

mail php imap

16/09 2015

2.0.3

2.0.3.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0

 

mail php imap

09/07 2015

2.0.2

2.0.2.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0

 

mail php imap

06/05 2015

2.0.1

2.0.1.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0

 

mail php imap

20/04 2015

2.0

2.0.0.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0

 

mail php imap

20/04 2015

1.1

1.1.0.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.3.0

 

mail php imap

20/04 2015

1.0

1.0.0.0 https://github.com/barbushin/php-imap

PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension

  Sources   Download

LGPL

The Requires

  • php >=5.3.0

 

mail php imap