Reading an email inbox with PHP – IMAP.

This is a short tutorial on how to connect to an IMAP email inbox using PHP’s imap functions. To keep things simple, I will connect to the inbox and then retrieve any emails that were received in the last week.

Let’s take a look at the following example:

<?php

//The location of the mailbox.
$mailbox = '{localhost:143/notls}';
//The username / email address that we want to login to.
$username = '[email protected]';
//The password for this email address.
$password = 'mypassword';

//Attempt to connect using the imap_open function.
$imapResource = imap_open($mailbox, $username, $password);

//If the imap_open function returns a boolean FALSE value,
//then we failed to connect.
if($imapResource === false){
    //If it failed, throw an exception that contains
    //the last imap error.
    throw new Exception(imap_last_error());
}

//If we get to this point, it means that we have successfully
//connected to our mailbox via IMAP.

//Lets get all emails that were received since a given date.
$search = 'SINCE "' . date("j F Y", strtotime("-7 days")) . '"';
$emails = imap_search($imapResource, $search);

//If the $emails variable is not a boolean FALSE value or
//an empty array.
if(!empty($emails)){
    //Loop through the emails.
    foreach($emails as $email){
        //Fetch an overview of the email.
        $overview = imap_fetch_overview($imapResource, $email);
        $overview = $overview[0];
        //Print out the subject of the email.
        echo '<b>' . htmlentities($overview->subject) . '</b><br>';
        //Print out the sender's email address / from email address.
        echo 'From: ' . $overview->from . '<br><br>';
        //Get the body of the email.
        $message = imap_fetchbody($imapResource, $email, 1, FT_PEEK);
    }
}

A drill-down of the PHP code above:

  1. We opened an IMAP stream to our inbox. Note that you will have to change the connection details to match your own inbox. It is worth pointing out that 143 is the default port for IMAP and that notls disables any TLS encryption.
  2. If the imap_open function returns a boolean FALSE value, then it means that we were unable to connect to the inbox in question. In the code above, I throw an Exception with the error message that is returned by imap_last_error.
  3. We then created our search criteria. In this example, I told the imap_search function to return emails that have been received in the last week. If you want to retrieve all emails that are currently in the inbox, then you will need to set the $search variable to ALL. If you only want to view unread emails, then you can set the $search variable to UNSEEN. It is important to note that the imap_search function returns a boolean FALSE value if no emails exist. In other words, it will not return an empty array.
  4. Finally, we loop through the emails and print out the subject and the sender’s email address. Note that we use FT_PEEK with the imap_fetchbody function because we want to read the email without flagging it as read! You can remove this parameter if you want your PHP script to mark unseen emails as being read.

Hopefully, you found this guide helpful!