A PHP class that makes working with IMAP as easy as possible.

This class is written to be chain-able so to create a logically fluent and easily readable way to access an IMAP mailbox.

It simplifies the PHP IMAP_* library into a set of easy to read methods that do the heavy lifting for you.

It has been fully tested to work with PHP 5.3+, including PHP 7.0.

Installation via Composer

You can now install this class via composer:

composer require benhall14/php-imap-reader

Remember to add the composer autoloader before using the class and use the correct namespace.

require 'vendor/autoload.php';
use benhall14\phpImapReader\Email as Email;
use benhall14\phpImapReader\EmailAttachment as EmailAttachment;
use benhall14\phpImapReader\Reader as Reader;


Please make sure you have added the required classes.

In its simplest form, use the following to connect:

# your imap user name
define('IMAP_USERNAME', '');

# your imap password
define('IMAP_PASSWORD', '');

# your imap address EG. {}
define('IMAP_MAILBOX', '');

# the path to save attachments to or false to skip attachments
define('ATTACHMENT_PATH', __DIR__ . '/attachments');


# create a new Reader object

# use one or more of the following chain-able methods to filter your email selection
# alias for mailbox($mailbox)

# sets the mailbox to return emails from. Default = INBOX

# retrieve a specific email by id

# get all RECENT emails

# get all FLAGGED emails

# get all UNFLAGGED emails

# get all UNANSWERED emails

# get all DELETED emails

# alias for UNSEEN()

# get all UNSEEN emails

# get all emails from $email

# get all emails with $string in the subject line

# get all emails with $string in the body

# get all emails with $string TEXT

# get all SEEN emails

# alias for SEEN()

# get all NEW emails

# get all OLD emails

# get all emails with $keyword KEYWORD

# get all emails without $keyword KEYWORD

# get all emails received before $date. *Date should be in a format that can be parsed by strtotime.*

# get all emails received since $date. *Date should be in a format that can be parsed by strtotime.*

# get all emails sent to $to

# get all emails with $string in the BCC field

# get all emails with $string in the CC field

# get all emails received on $date. *Date should be in a format that can be parsed by strtotime.*

# limit the number of emails returned to $limit for pagination

# used with limit to create pagination

# order the emails returned in ASCending order

# order the emails returned in DESCendeing order

# get all emails (default)

# finally make the connection and retrieve the emails.

# You can then loop through $imap->emails() for each email.
foreach($imap->emails() as $email){

# The email has been clean and formated.
# see below.


# ... your code here ...

} catch (Exception $e){

echo $e->getMessage();


While looping through the returned emails, each email object can be used as below:

# Return true if the email is to $email, else returns false

# Returns an array of Reply To email addresses (and names)

# Returns an array of CC email addresses (and names)

# Returns the recipient email address

# Returns the id of the email

# Returns the size of the email

# Returns the date in the $format specified. Default Y-m-d H:i:s

# Returns the email subject

# Returns the sender's name, if set.

# Returns the sender's email address

# Returns the plain text body of the email, if present

# Returns the html body of the email, if present

# Returns true/false based on if the email has attachments

# Returns an array of EmailAttachment objects

# Returns an attachment based on the given attachment $id

# Returns true/false based on the recent flag

# Returns true/false based on the unseen flag

# Returns true/false based on the flagged flag

# Returns true/false based on the answered flag

# Returns true/false based on the deleted flag

# Returns true/false based on the draft flag

The $email->attachments(); method returns an array of attachments belonging to the email in a benhall14\phpImapReader\EmailAttachment object. The following methods are available for each attachment.

# check if the current $email has any attachments.

# get the attachments for the current $email in the loop.
$attachments = $email->attachments();

# loop through the found attachments.
foreach($attachments as $attachment){

$attachment->id(); # Returns the attachments ID.

$attachment->name(); # Returns the attachments name.

$attachment->filePath(); # Returns the local file path for the attachment. This is based on the ATTACHMENT_PATH constant set in the imap config.

$attachment->content(); # Returns the attachments content data.

$attachment->type(); # Returns either 'attachment' or 'inline'.




Works with PHP 5.5, PHP 5.6, and PHP 7+


Copyright (c) Benjamin Hall, -

Licensed under the MIT license


If you find this project helpful or useful in any way, please consider getting me a cup of coffee - It's really appreciated :)


More Projects?
View Code Showcase
Benjamin, is brilliant at what he does, he really knows what he is at. Gives real useful advice and guidance. He is fast and timely provides updates and always provides solutions and listens well. Trust me Benjamin is a breath of fresh air.
- Alexander T.
Ben had a great idea for my project and completed the project to our requirements very quickly. He also helped me tweak a few issues and answered my questions quickly. We'll work with Ben again!
- Gordon S.
I was impressed: a freelancer who could give me inputs and details on how he'd implement the solution, kept me updated in the meanwhile, understood my requirements and proposed pretty nice tweaks to make things even better. Ben, you are amazing. I will need no more time to find any other dev's, and will surely deal with you again if I need something related to these things in the future. Full stop.
- Marco F.
Absolutely superb, a great work ethic, outstanding knowledge and a great eye for detail. I thoroughly recommend him!
- Gary J.
Benjamin is a talented developer who can tackle technically challenging projects with great service and communication. Thoroughly recommended.
- Steven M.
Ben truly went above and beyond. He's fantastic at communication, coding, getting round problems, fixing issues, WordPress, custom plugins and much more. I will be using Ben again for other projects.
- Rachael R.
Ben went the extra mile and not only resolved the problem but provided a more suitable solution for the long term. Cheers!
- William R.
Ben is an amazing developer. He took the project and delivered it on time and within budget, whilst maintaining excellent communication. He also worked beyond what was asked to make sure the project was functioning correctly. Would highly recommend A++++!
- David B.
Fantastic work on our JS script. Ben worked beyond the goals of the project and overcame extra issues that were not defined in the project. He is very quick to respond and patent when waiting for our response. One of our top 5 Developers, We had to deal with many Interfaces including server firewalls/Java-scripting/JSON/PHP/PHP APIs/Remote Databases and automation.
- Kevin J.
Ben is just great to work with. Efficient, clearly communicating, understanding very quickly, and also giving great advice. I will continue working with Ben on any other WordPress stuff. Sorry for all those other PPH'ers :-)
- Bruno V.
Absolutely amazing experience. Ben is an expert in this field, I was amazed at how quickly he jumped on our platform and decoded the code, db etc. He will go over and above to find solutions for clients. Highly recommended.
- Harrus A.
Interested in working together to make something great? Hire CONOBE