Why do you want to do that?

is added to make listing mail folders easier. When the client asks for a list of mail folders, it can provide the IMAP server with a prefix to the filenames. It works just like the shell: if you say

"docu*"

you will match all filenames starting with the string "docu".

The

$M_PREFIX

enables us to tuck a prefix onto the actual filenames of our folders, thus making it easy to tell the IMAP server that we want it to list anything that starts with the prefix as if it were a mail folder:

$mailboxes = imap_listmailbox($MBOX, $M_MBOX, "$M_PREFIX%");

The third argument to the PHP3 function

imap_listmailbox()

tells the IMAP server to only list filenames starting with the chosen prefix. Before showing the name to the user we massage the name a bit, removing the prefix and the host/port/service information.

In short: folder names on disk have prefixes so we can identify them. When displayed to the user everything but the folder name itself is stripped away.

Keeping track

Now that we have several folders, we have to keep track of which folder we're in. Actually, most of the code to do this was already added in version 1.0. The

$m

variable is used to keep track of what folder is currently being viewed.

To remember what folder we were in while browsing a message, when moving or deleting messages some pages have a hidden input field (well, "input field" is obviously a bad name if you're not supposed to input anything into it or even see it, but nevertheless, 'tis an input field). This is sort of a sneaky way to have the application open the correct folder.

We could also have used cookies to keep track of the current folder. Setting cookies is fairly simple in PHP3 and reading them is even simpler. (Yes, you guessed it; cookies are turned into variables just like forms fields are).

Note: See page 5 for a rundown of what's new in this tutorial.

What's next?

Our little Webmail interface is still under 500 lines of code. Considering what it would take to write an equivalent application with a GUI, that is not bad at all. Of course there is a lot left to be desired. For one, the folders should be hierarchical and they should be displayed as such. Some tasks could be automated, such as the creation of monthly archive folders. E-mail could be moved from the default folder into such archive folders when a message reaches a certain age.

Bjørn Borud works as a systems engineer at Fast Search and Transfer ASA, which develops large-scale search engines and cutting-edge image compression software. In his copious spare time Bjørn reads, writes software, and sometimes writes articles for various publications.

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.