Here’s some simple code to show how you open an IMAP mailbox and display the folder names in PHP. This covers the basics of opening the IMAP mailbox, getting a list of folder names and displaying them in a clickable list.

Some people might say “IncrediBILL, you’re a bit late to the PHP IMAP party as people have covered it in many places.” and I might reply “True. However, there’s a lot of SHIT out there that I had to weed through to figure out the PHP implementation and I worked at Lotus cc:Mail back when the our team contributed to the initial specs that later became IMAP and I had trouble with this cluster fuck PHP implementation, not that I’m complaining mind you.”. Seriously folks, I used to be in the catbird seat of Windows Email back in Win 3.0, 3.1 and ME before I moved along to another startup, so I know this shit inside and out and invented some things you all take for granted but that’s old news and maybe a sour grape or two. Believe it or not, some of my ex-coworkers are still working on Yahoo!’s mail client 25 years later. Talk about being stuck if a rut. Sheesh.Watch Full Movie Online Streaming Online and Download

But I digress from the code at hand.

FWIW, I get all this digressing from my mother who can digress from digressing in a digression and somehow still manages to come out of her nested digression loops many minutes later while the drool pools under anyone still listening to her, or awake for that matter, but I digress.

PHP IMAP Mailbox Paths

Instead of using just the plain IMAP path and then function calls to fill in required attributes, the IMAP mailbox path includes all the parameters like a command line Linux utility would need.

The following are some common host names for various IMAP inboxes that will help speed you along. Please submit more IMAP connection strings if you know of any to help others speed their way to accessing their email account from PHP.

  • Gmail
  • Yahoo
  • AOL
  • Horde used by Plesk control panel;

PHP IMAP Sample Code to Connect to Host and Display Folders


if ($mbox=imap_open( $host, $user, $pass ))
$imap_obj = imap_check($mbox);
echo "<h1>CONNECTED TO IMAP HOST</h1><h2>$host (".  $imap_obj->Nmsgs  .")<h2>";
} else
echo "<h1>FAILED TO CONNECT TO IMAP HOST!</h1>\n";

echo "<h3>IMAP LIST OF FOLDERS</h3>";
$folders = imap_list($mbox, $host, "*");
echo "<ul>";
foreach ($folders as $folder) {
echo '<li><a href="mail.php?folder=' . $folder . '&func=view">' . imap_utf7_decode($folder) . '</a></li>';
echo "</ul>";

There you have it, the first installment that opens the raw inbox and displays the raw folder names. In the next part we’ll strip the folder names to more friendly and add some additional functionality.

Rambling about off topic stuff

Yes, I know the code numbering in the display about is skewed a line but
there appears to be a bug in SyntaxHighlighter Evolved when used with anything based on Twitter Bootstrap. If I switch to other themes it’s OK, when I switch back to something based on Boostrap it’s borked. Probably some simple CSS thing but I can’t find anything about it and I’m not wasting time debugging it at the moment so I decided to just post it as-is. Getting the code out is more important than fixing that stupid little glitch.

PART 1: How to Open an IMAP Mailbox and Display Folders in PHP
PART 2: Decoding IMAP UTF8 and iso-8859-1 Encoded Folders, Subjects and More
PART 3: Making Friendly IMAP Folder Names
PART 4: Reading and Displaying IMAP Message Headers
PART 5: Decoding and Displaying IMAP Messages Including HTML Messages
PART 6: Sending IMAP Messages, Reply, Reply to All and Forward
PART 7: Simple IMAP Email Reader with Download

3 thoughts on “PHP IMAP EMAIL PART 1: How to Open an IMAP Mailbox and Display Folders in PHP

  1. For performance I would even go as far as to have it cache the emails to reduce load if you are offering email hosting. Perhaps refreshing the ache every 5 minutes. You could also include a purge folder feature.

    Very good tutorial mate.


Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>