HomePHP Page 7 - Building A PHP-Based Mail Client (part 1)
Back To Square One - PHP
Ever wondered how Web-based mail clients work, or what happens toyour email after you hit the "Send" button? This three-part case studydelves into the wild and wacky world of Web-based email applications, usingPHP's built-in POP3 functions to build an email client suitable forrerieving POP3 email via a Web browser. In this introductory segment -connecting to a POP3 server, logging in and out, retrieving message headersfor display, and deleting messages off the server.
Now, that entire effort was a pretty major exercise - especially if, like me, you hadn't done it before. And I'm not done yet - I still need to figure out how to handle attachments. But before I go there, I'd like to close up this first part with a look at, appropriately enough, the "logout.php" script.
You'll remember, from a couple pages back, that the page header includes some code to display a link to log out of the system. This link points to "logout.php", an extremely simple script which destroys the session created at the time of logging in, and sends the browser back to the application's index page.
Take a look:
<?
// logout.php - destroy session
// destroy session variables and send back to login page
session_start();
session_unregister("SESSION");
session_unregister("SESSION_USER_NAME");
session_unregister("SESSION_USER_PASS");
session_unregister("SESSION_MAIL_HOST");
header("Location:index.php");
?>
If you look at "login.php" again, you'll see that I'm simply
destroying, via session_unregister(), the session variables created at login time. This is necessary to avoid having one user's sensitive account information "inherited" by subsequent users.
Once the session has been destroyed, the browser is redirected back to the index page. And so the cycle continues...
That's about it for this opening segment. In this article, you learned a little bit about the basics of designing software applications - namely, putting down requirements on paper, separating common elements into a single location, and keeping lots of caffeine handy. You also got an introduction to PHP's IMAP functions, using built-in IMAP constructs to connect to a POP3 server and obtain a detailed message listing from it. Finally, you learned a little about PHP's session management functions, with code illustrations of how to create, use and destroy session variables.
I still have a long way to go before this application is complete. My primary problem right now is understanding how to handle attachments, both so that I can display (and download) them, and so that I can attach them to new messages or replies. I plan to bone up on a little theory before attempting this - come back next week and I'll tell you what I find out.
Note: All examples in this article have been tested on Linux/i386 with Apache 1.3.12 and PHP 4.0.6. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!