If you look at the argument list of you will see that we assign a value to the argument. This is a way of setting a default value for the argument so that if we call the function without that argument it is assigned a default value. Also note that at the beginning of the function there are two lines beginning with the keyword "global" followed by a list of variables. Per default all variables used in the function are only valid within the scope of the function. To access global variables you have to declare them as such.
Next, if you look at where we call the function you see that we prefixed the function name with a "@". This means that PHP should suppress error messages that may occur when calling this function.
Listing contents of a mailbox
After you have logged in the Web mail interface will show you a listing of your default mailbox and some buttons at the bottom of the browser window. On my machine this looks like this. For each message in your mailbox the Web mail interface shows you the sender, the subject, and the date it was sent. To the left of every message there's a small checkbox. If you wish to delete several e-mail messages at a time you just check them and push the delete button.
The function in the file is responsible for generating the listing.
Viewing a message
To view a message you click its subject in the mailbox listing. The Web mail interface will then call the function to show you the message. This may look something like this. The Web mail interface does very little processing before displaying a mail message so things might look a bit weird if you get a lot of multipart mail messages etc., but for educational purposes it'll do just fine. Sending a message
There are basically two ways to send a message. You can either click the "compose" button on the mailbox listing page to create a new message, or you can click the "reply"-button on the message display page. If you choose the latter you will get a message composition form with the recipient already filled in and the message you are replying to glued in and quoted in the text area where you type in your mail. However, if you choose the compose button on the mailbox listing page you get an empty form.
The function is used to present both the empty mail form and the one that has been filled out as a reply to a mail message. The function checks to see if the optional arguments and have been set. If they have been set it fetches the message the user is replying to and quotes it. Otherwise it just puts up a blank mail form.
Once the message has been composed the browser sends the contents of the mail form back to the webmail interface which then calls the function to actually send the message.
What can be made better?
A healthy dose of paranoia should tell most people that the solution has to be secured somehow. Especially if users are to access it from outside the corporate network. Probably the easiest way to improve security is to install an SSL-enabled Apache server so all the traffic between the user and the server is encrypted. Most IMAP clients implement various methods to access, list, and manipulate mail folders on the server. It shouldn't be too hard to add this to our interface. Most of the code for listing mail folders and browsing messages in them is already there. (Notice how it makes a point of remembering what folder it was playing around in although the folder name is blank. The blank folder name is mapped to the default mailbox).
Handling attachments and multipart MIME messages isn't too hard to add either. Most of the functions you need to figure out the structure of such mail messages are already present in the IMAP library that PHP uses.
If your organization uses LDAP to keep track of information about its employees, organization, access privileges, and so forth, a natural extension of the Web mail interface would be to integrate it with LDAP. And of course, PHP can be built with support for LDAP.ø
Related resources