Thanks to PHP, we can check our email account remotely using PHP and its imap_xxx functions, which allow us to communicate with mail servers via IMAP, POP3 or NNTP protocols. In this article Mitchell shows us how to create a completely web-based email checking script which can also delete, send and reply to emails... all using only PHP's IMAP functions.
Create Your Own Mail Script With PHP and IMAP - What is IMAP? (Page 2 of 6 )
IMAP is a method of accessing email messages stored on a remote mail server. The reason IMAP is so popular is because it allows clients to connect to and interact with their mail server without the need to download their emails and files locally. In other words, IMAP allows us to treat a remote mail server as is if it was on our local machine, checking our accounts, manipulating emails, replying to emails, etc.
IMAP shouldn't be confused with the POP3 protocol, however. If you've ever used Outlook or Eudora or any other client mail access program, then you'll know that you can "check" your email, disconnect from the Internet and your mail will be stored on your local machine, saving you the hassle of being on the 'net to read your email. This is POP's biggest advantage: offline access to your email. POP downloads all new messages off the server onto your local PC and then deletes them from that server. IMAP on the other hand queries the server, leaving all emails intact unless you specifically tell it to delete them.
POP was designed for use with client side email applications such as Outlook, whereas IMAP was designed for people on the move, wanting to check their email but not necessarily retrieve it.
The key goals for IMAP are:
Be fully compatible with Internet messaging standards, e.g. MIME.
Allow message access and management from more than one computer.
Allow access without reliance on less efficient file access protocols.
Provide support for online, offline and disconnected access modes.
Support for concurrent access to shared mailboxes.
Client software needs no knowledge about the server's file store format.
Let's take the last point into configuration: "Client software needs no knowledge about the server's file store format". In my opinion this is one of IMAP's best features. The client simply queries the mail server using IMAP specific commands and that server returns the data the client's after.
It doesn't matter how the server stores the actual email messages (whether it be in a flat file, a MySQL database, an MSSQL server database or any other format) as long as the server returns the data that the client requests, then the storage method and location are irrelevant.
Bringing PHP into the picture
If you've worked with PHP extensions then you'll know that it supports IMAP through an extension library, which isn't installed by default. Before we can play around with PHP and IMAP, we have to install the necessary IMAP libraries first.
Configuring PHP on Windows with IMAP support
It's easy to configure your Windows server to work with IMAP. Firstly, grab the php_imap.dll file from your PHP download (it should be in the extensions directory). If you can't find it then download the PHP zip package here.
Next, copy php_imap.dll to your PHP extension directory, which you can find in your php.ini file as the extension_dir variable. Lastly, open php.ini and look for the line starting ";extension=php_imap.dll". Remove the semicolon from the front, save the file and restart your web server.
Configuring PHP on Linux with IMAP support
To enable IMAP support on your PHP installation you need to compile PHP with the --with-imap argument. You also need to download the IMAP library here. Once you've downloaded it, copy c-client/c-client.a to /usr/local/lib/libc-client.a. Also copy c-client/rfc822.h, mail.h and linkage.h to /usr/local/include or some other directory in your include path.
Testing for IMAP support
To make sure IMAP support is configured correctly, create a new file called phpinfo.php and save it into a directory that your web server can process. Add the following code to it and run it in your web browser:
<?php echo phpinfo(); ?>
Scroll down the page about half way and look for the IMAP section. You should see something like this:
Now that we've setup IMAP support, it's time to take a look at the various IMAP functions that we will be using later in this article to create our email script.