PHP

Create Your Own Mail Script With PHP and IMAP

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.

The IMAP library has 61 functions. Luckily we only need to use a handful of these to create a fully functional email script. The functions we will be using in this article are shown and detailed below:

imap_open: Opens an IMAP stream to a mailbox.

imap_headers: Returns headers for all emails in a mailbox.

imap_headerinfo: Reads the header of an email.

imap_delete: Mark an email for deletion from current mailbox.

imap_expunge: Delete all emails marked for deletion.

imap_mail: Sends an email message to a recipient.

imap_body: Reads the body of an email from a mailbox.

As mentioned earlier, we're going to create a script that allows us to check our email account on the move using IMAP. Before we create the script however, let's start with the basics. Create a new file called testimap.php and enter the following code into it:

<?php

$server = "mail.com";

$user = "john";

$pass = "mypass";

$conn = @imap_open("\{$server/imap}INBOX", $user, $pass)

or die("Connection to server failed");

?>

Replace the $server variable with the host name of your mail server. Replace $user with your mailbox account name and also replace $pass with the password for your mailbox. To connect to our mail server we use the imap_open function, passing in our mailbox, user ID and password. The format of the mailbox parameter looks like this:

{host_name[:port number]/imap}INBOX

The port number part of the mailbox parameter is optional. If we refer back to the testimap.php file above, our mailbox parameter looks like this:

{mail.com/imap}MAILBOX

In our example we've left out the port number. We could've optionally specified the port number like this:

{mail.com:25/imap}INBOX

The imap_open function returns an IMAP stream on success and FALSE on failure. We use this returned IMAP stream with various other imap_xxx function calls to manipulate our mailbox, send emails, etc.

To retrieve a list of emails in our mailbox, we can use the following code:

$headers = @imap_headers($conn)

or die("Couldn't get emails");

$numEmails = sizeof($headers);

echo "You have $numEmails in your mailbox";

In the example above I've called the imap_headers function, passing in our IMAP stream as the only parameter. The imap_headers function returns an array of headers. Each of these headers contains the details of one email in our mailbox, and we can use the values of these headers to show who these emails have come from, their subjects, date received, etc. These headers do not however contain the body of each email message, and we must use the imap_body function to get them.

Now that we've got an array of headers representing each email in our mailbox, we can loop through them to display the details of each message with the imap_headerinfo function, like this:

for($i = 1; $i < $numEmails+1; $i++)

{

$mailHeader = @imap_headerinfo($conn, $i);

$from = $mailHeader->fromaddress;

$subject = strip_tags($mailHeader->subject);

$date = $mailHeader->date;

echo "Email from $from, subject $subject, date $date<br>";

}

As you can see from the code above, we use the imap_headerinfo function to get the header details for each message. We extract the from address, subject and date for each message and use the echo command to output them to the browser.

Now that we've displayed each message in our mailbox, we can use the imap_close function to close the IMAP stream we created earlier with imap_open:

imap_close($conn);

On this page we've taken a look at the basic imap_xxx functions that are required to open an IMAP stream, retrieve headers and display the details of each email from these headers. On the next page we're going to put everything we've learnt so far to good use and create the email script I've been mentioning.