• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
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.

Check Out These Related posts:

  1. Using PHP IMAP functions to download email from Gmail
  2. Parsing Google Analytics data with PHP: A Series
  3. Using PHP IMAP functions to download email
  4. Install PHP IMAP on CentOS

Filed Under: PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap