In this four-part series of articles we are going to build an email client with PHP. I will try to implement a full email client with all the functionality that you would expect from an email client, which includes a trash folder, sent items folder and a draft folder. But that's not all.
I will be using PHP's imap functions to write this client. There are over sixty functions available that you can use to create a mail client for both POP3 and IMAP. Visit http://www.php.net/ to see what functions are available. We will be creating a mail client that supports POP3 because it is by far the most popular mail protocol used today. To send an email message with PHP is very simple. You just use PHP's mail() function. The syntax for the function is:
But because we want to give the user the choice to send attachments with email, we are going to use the freely available PHPMailer class (from http://phpmailer.sourceforge.net ). This class gives us the power to send email messages with all kinds of features, such as attachments, HTML messages or just plain messages. Although I will be using PHPMailer in this article, I will not be using it extensively, so please look at the following sites to learn more about this class:
To retrieve email messages from the mail server, we will be using the @imap_headerinfo() and imap_msgno( ) functions. The imap_headerinfo() function retrieves all the message headers such as from, subject, and so forth, and the imap_msgno() function identifies the message number as it is numbered on the server.
To connect to the mail server we use the imap_open($mbox, $userid, $userpassword) function. As you can see this function takes three parameters, the mailbox name, the user name and user password.
The application will provide the ability to do the following:
Connect to the mail server and download and process messages.
Store the messages in the database one by one.
Retrieve and display all the newly downloaded messages.
After the messages are displayed on the index page, the program will give the user the option to view a message (this will lead to the view.php page) or to delete the message.