http://www.developer.com/lang/php/article.php/610151/Web-mail-in-PHP.htm
A Web-based e-mail interface may not be a very original idea, nor is my humble example any match for the more advanced Web mail applications that already exist, but I'm likely to need such an interface at some point and it makes an excellent topic for showing off what you can do with PHP. So Web mail it is. PHP also supports a number of databases, ranging from big names like Oracle, Sybase and Informix to smaller database systems like MySQL -- the latter being perhaps the most popular database to use in conjunction with PHP. As I mentioned earlier PHP lets you mix HTML and script code. Now what does this look like in practice? Consider the following stripped-down file: As you can see, we start out with some standard HTML code. We provide a title for the page and a heading. Then we encounter section of the document enclosed within "<?PHP" and "?>". These are called SGML processing tags -- this is how we tell the Web server where there's code that we wish to run. The code doesn't do much. First it calls the The ACTION parameter points to a file with the peculiar suffix Remember that we had two input fields in our form, named "name" and "email"? The values we typed into these input fields are now assigned to PHP variables with the same names as the form input fields and we can manipulate them just like any other variable. In this particular case we combine them into a string and use a built-in function to mail the data to the Webmaster.
Web mail in PHP
April 16, 1999
Asked to write an article about Apache and PHP, I had to take a moment to come up with a good sample application to demonstrate the beauty of Apache and PHP. It had to be something that I would personally like to use but simple enough for educational purposes, and something at least marginally original. What is PHP?
In short, PHP is a server-side programming environment that lets you mix HTML and script code. The scripting language resembles Perl and if you know a little Perl or C you will find it quite easy to get started using PHP. Since PHP was specifically designed to be a server-side scripting language for Web servers, it offers a vast array of functions and behind-the-scenes magic to make common tasks easy. For instance, data entry fields in forms will automagically turn up as PHP variables that use the same names as the form fields etc. Enough talk!
I'm sure you're dying to get a taste, so let's jump right into some simple code.
<TITLE>Sample PHP script</TITLE>
<H1>Date sample</H1>
<?PHP
$today = date("Y-m-d");
print "Today's date is $today\n";
?>
The above date was printed by PHP.
function with some parameters to tell it how we would like the current date formatted. The result of date()
is assigned to variable date()
and on the next line we print the result. $today
Forming content
As mentioned earlier, PHP was specifically designed as a server-side scripting language to make life easier for Web developers. Something a lot of people ask about is how to handle FORM content, so let's see how that can be done in PHP. Consider the following HTML code that contains a form:
<TITLE>Form example</TITLE>
<FORM ACTION=formhandler.php3 METHOD=GET>
Name: <INPUT TYPE=text NAME=name VALUE=""><BR>
Email: <INPUT TYPE=text NAME=email VALUE=""><BR>
<INPUT TYPE=submit>
</FORM>
. This is the file containing the code that handles the form content when we click the submit button. The code for this handler might be something like this: php3
<TITLE>Form handler example</TITLE>
Mailing the data to the webmaster:
<?PHP
$message = "Name: $name\nEmail: $email\n";
mail("your-email-here", "Form feedback", $message);
print "The following data was sent:\n $message";
?>
Going from here to there
We could spend all afternoon looking at cute little examples like that, but that's probably not why you came. You're wondering whether this PHP thing can be used for something useful and worthwhile.
| 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. 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. Logging in and opening the default mailbox for the user is done by calling the The username is obtained from the built-in variable If the username is not set or the attempt to log onto the IMAP server failed When the login is successful, |
| Also note that at the beginning of the function Next, if you look at where we call the 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 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 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 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.ø Bjørn Borud is a research scientist in Norway at Fast Search and Transfer ASA, a company that develops large-scale search engines and image compression software. In his copious spare time, Bjørn reads, writes software, and contributes to various publications. |