Web mail in PHP

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).

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

imapfuncs.phl

and the part that implements the main page and the general flow of things, called

index.php3

. 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

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

m_login()

function defined in the

imapfuncs.phl

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

m_login()

just returns and does nothing further.

The username is obtained from the built-in variable

$PHP_AUTH_USER

, 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

m_reject()

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,

m_login()

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

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

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