Home / Open a mailbox other than the INBOX with PHP IMAP

Open a mailbox other than the INBOX with PHP IMAP

This post is part of an on-going series on this blog about connecting to IMAP mailboxes with PHP, ultimately showing how to download and parse reports from Google Analytics. When logging into an IMAP server with PHP the default mailbox is used – usually the INBOX. This post looks at how to list the mailboxes for the IMAP account and then how to connect to one of the other mailboxes.

Listing the mailboxes

The examples in this post will use a Gmail account as an example. In Gmail you can create "Labels" which are displayed as mailboxes when you log in using IMAP.

We need to use the server connection string multiple times when using the PHP IMAP functions to change mailboxes, so it’s a good idea to save it as a variable. In the examples below we’ll connect to Gmail so the server string would be defined like so (refer to my using PHP IMAP functions to download email from Gmail post for more details):

$server = '{imap.gmail.com:993/ssl}';

Then we connect to the server:

$connection = imap_open($server, $login, $password);

To get a list of mailboxes use the imap_list() function like so:

$mailboxes = imap_list($connection, $server, '*');

The third parameter is a pattern match and the * indicates to return all mailboxes. You could for example just pass ‘[Google Mail]*’ to only show the Gmail mailboxes and not your own custom labels.

Doing print_r($mailboxes) will show something like this:

Array
(
    [0] => {imap.gmail.com:993/ssl}INBOX
    [1] => {imap.gmail.com:993/ssl}Personal
    [2] => {imap.gmail.com:993/ssl}Servers
    [3] => {imap.gmail.com:993/ssl}[Google Mail]/All Mail
    [4] => {imap.gmail.com:993/ssl}[Google Mail]/Bin
    [5] => {imap.gmail.com:993/ssl}[Google Mail]/Drafts
    [6] => {imap.gmail.com:993/ssl}[Google Mail]/Sent Mail
    [7] => {imap.gmail.com:993/ssl}[Google Mail]/Spam
    [8] => {imap.gmail.com:993/ssl}[Google Mail]/Starred
)

In the above example, INBOX is the default inbox, "Personal" and "Servers" are some custom labels I’ve set up and the rest are Gmail’s special mailboxes.

To loop through and display just the names and not the server, you could do something like this, stripping out the server name using str_replace():

foreach($mailboxes as $mailbox) {
    $shortname = str_replace($server, '', $mailbox);
    echo "$shortnamen";
}

and the resulting list would look like this:

INBOX
Personal
Servers
[Google Mail]/All Mail
[Google Mail]/Bin
[Google Mail]/Drafts
[Google Mail]/Sent Mail
[Google Mail]/Spam
[Google Mail]/Starred

Connecting to a mailbox

Use the imap_reopen() function to connect to a different mailbox. You need to pass the already established connection and then the mailbox to connect to. The mailbox name must include the server string. The following example connects to the ‘Servers’ mailbox:

imap_reopen($connection, $server.'Servers');

You can then use the other IMAP functions to loop through mail in that mailbox and display messages from it. I covered how to loop through a mailbox in an earlier post so the you’d just need to connect to the mailbox and then loop through the messages in the normal way.

Future posts

The next post in this series, this time next week, will look at how to loop through the mailbox looking for a specific message based on the subject. The following two posts will then extract the Google Analytics data from the emails so you could inject into a database etc. I’ll then look at other IMAP functions and also some other libraries for connecting to email accounts with PHP. Be sure to subscribe to my RSS feed (details below) so you don’t miss out on this series.

Read about the series here.