PHP IMAP Reader

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;

Usage

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. {mail.example.com:993/novalidate-cert/ssl}
define('IMAP_MAILBOX', '');

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

try{

# create a new Reader object
$imap = new Reader(IMAP_MAILBOX, IMAP_USERNAME, IMAP_PASSWORD, ATTACHMENT_PATH);

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

# sets the mailbox to return emails from. Default = INBOX
->mailbox($mailbox)

# retrieve a specific email by id
->id($id)

# get all RECENT emails
->recent()

# get all FLAGGED emails
->flagged()

# get all UNFLAGGED emails
->unflagged()

# get all UNANSWERED emails
->unanswered()

# get all DELETED emails
->deleted()

# alias for UNSEEN()
->unread()

# get all UNSEEN emails
->unseen()

# get all emails from $email
->from($email)

# get all emails with $string in the subject line
->searchSubject($string)

# get all emails with $string in the body
->searchBody($string)

# get all emails with $string TEXT
->searchText($string)

# get all SEEN emails
->seen()

# alias for SEEN()
->read()

# get all NEW emails
->newMessages()

# get all OLD emails
->oldMessages()

# get all emails with $keyword KEYWORD
->keyword($keyword)

# get all emails without $keyword KEYWORD
->unkeyword($keyword)

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

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

# get all emails sent to $to
->sentTo($to)

# get all emails with $string in the BCC field
->searchBCC($string)

# get all emails with $string in the CC field
->searchCC($string)

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

# limit the number of emails returned to $limit for pagination
->limit($limit)

# used with limit to create pagination
->page($page)

# order the emails returned in ASCending order
->orderASC()

# order the emails returned in DESCendeing order
->orderDESC()

# get all emails (default)
->all()

# finally make the connection and retrieve the emails.
->get();

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

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

}

# Reset the reader and connect to another folder.
$imap->reset()->folder('Sent')->get();

# You can also create a folder/mailbox on the IMAP stream.
$imap->createFolder('New Folder Name');
#or
$imap->createMailbox('New Folder Name');

# You can also check if a mailbox/folder exists on the IMAP stream using:
if ($imap->doesMailboxExists('INBOX')) {
return "Yes, it exsits";
} else {
return "No, it doesn't exist.";
}

# ... 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
$email->isTo('mail@example.com');

# Returns an array of Reply To email addresses (and names)
$email->replyTo();

# Returns an array of CC email addresses (and names)
$email->cc();

# Returns the recipient email address
$email->to();

# Returns the id of the email
$email->id();

# Returns the size of the email
$email->size();

# Returns the date in the $format specified. Default Y-m-d H:i:s
$email->date($format);

# Returns the email subject
$email->subject();

# Returns the sender's name, if set.
$email->fromName();

# Returns the sender's email address
$email->fromEmail();

# Returns the plain text body of the email, if present
$email->plain();

# Returns the html body of the email, if present
$email->html();

# Returns true/false based on if the email has attachments
$email->hasAttachments();

# Returns an array of EmailAttachment objects
$email->attachments();

# Returns an attachment based on the given attachment $id
$email->attachment($id);

# Returns true/false based on the recent flag
$email->isRecent();

# Returns true/false based on the unseen flag
$email->isUnseen();

# Returns true/false based on the flagged flag
$email->isFlagged();

# Returns true/false based on the answered flag
$email->isAnswered();

# Returns true/false based on the deleted flag
$email->isDeleted();

# Returns true/false based on the draft flag
$email->isDraft();

# Returns the email in .eml format
$email->eml();

# Saves the email in .eml format
$email->saveEml($filename);

# Returns number of emails in folder
$email->count();

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.
if($email->hasAttachments()){

# 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'.

}

}

Requirements

Works with PHP 5.5 through to PHP8.1+

License

Copyright (c) Benjamin Hall, ben@conobe.co.uk - https://conobe.co.uk

Licensed under the MIT license

Donate?

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

Donate

More Projects?

View Code Showcase