March 7, 2021
Hot Topics:

Web mail in PHP

  • By Bjxrn Borud
  • Send Email »
  • More Articles »

Now for the simple Web interface to a mail system. There are a number of ways to do this. We could write a system that runs with superuser privileges, mucks about with people's files directly, and totally ignores the fact that people actually have invented several different kinds of wheels already. But we won't. We'll use IMAP, also known as the Internet Message Access Protocol.

Mapping the interface

Why IMAP? Well, first of all IMAP is an open standard. The specs are publicly available to anyone and there are several implementations available in source code form. This means more developers can make IMAP-enabled software, which in turn gives the user more choice.

Second, IMAP is well suited to manage mailboxes on remote hosts rather than merely fetching mail to a local machine. With IMAP you can list mailboxes, copy or move mail between them, and request parts of multipart e-mail messages -- all on the server machine.

The idea here is that the user can use the Web interface if she likes to, but she can also access the same e-mail through her mail client. Using her favorite mail client, like Mozilla or Pine, is probably what you would prefer most of the time, but what about when you're on the road and you just want to check your e-mail from an Internet Cafe halfway around the world? Then a Web interface is really neat. All you need is a browser.

The prerequisites

Before you start, here's the shopping list:
  • The Apache Web server: apache_1.3.6.tar.gz
  • The IMAP server and libraries: imap-4.1.FINAL.tar.Z
    (Yes, I know the PHP documentation says you should get the latest version, but the latest didn't work with PHP for me so I reverted to one that had worked for me in the past).
  • PHP 3.0.7: php-3.0.7.tar.gz
You can do an exact search for these files on
to locate a copy near you.

Start by reading about the IMAP support in PHP. Compile the IMAP package and install the IMAP server and the libraries so PHP can link them when you build PHP. Then unpack the PHP source and read the file named INSTALL.DSO. This file should guide you through setting up PHP as a loadable Apache module.

When you have Apache with PHP3 and the IMAP libraries up and running, you can go ahead and install the Web mail interface and start playing with it.

How the code works

The code is pretty simple and straightforward. For simplicity it is divided into two parts: a library part, called
and the part that implements the main page and the general flow of things, called
. The latter is the file you point your browser at after installing the code. To make Apache accept PHP files as directory indexes you can edit your
so it contains the line:
   DirectoryIndex index.php3 index.html
When you install the code remember to
change these parameters to appropriate values:
    /* configurable parameters */

    $M_HOST        = "localhost";
    $M_MAILSERVER  = "transit.fast.no";
The first variable tells the Web mail interface where the IMAP server is running. The second line is used to tell the interface what domain name it should tuck onto the end of the username for outgoing mail.

Logging in

To limit access to the mailbox we use standard HTTP authentication, where the user logs in with her normal username and password on the system (make sure the user exists on the system and can access her mail through the IMAP server).

Logging in and opening the default mailbox for the user is done by calling the

function defined in the
library file. This function checks to see if we've already logged in and whether we have given a username. If we're already logged in
just returns and does nothing further.

The username is obtained from the built-in variable

, which contains the username you typed in when you got the standard username/password dialog from your browser.

If the username is not set or the attempt to log onto the IMAP server failed

is called. This function sends a header back to the browser telling it that it needs a username and a password, or that the username and password supplied was wrong.

When the login is successful,

stores the mailbox handle in a global variable for later use and then returns.

Page 2 of 3

This article was originally published on April 16, 1999

Enterprise Development Update

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

Thanks for your registration, follow us on our social networks to keep up-to-date