ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Read your e-mail with PHP!

(Page 3 out of 3)

The only problem is that some headers use multiple lines, and we'll have to account for that. The following code does the trick:

// Parse headers
$headers = array();
foreach ($header as $line) {
        $colon_pos = strpos($line, ':');
        $space_pos = strpos($line, ' ');

        if ($colon_pos === false OR $space_pos < $colon_pos) {
                // attach to previous
                $previous .= "\r\n" . $line;
                continue;
        }

        // Get key
        $key = substr($line, 0, $colon_pos);

        // Get value
        $value = substr($line, $colon_pos+2);
        $headers[$key] = $value;

        $previous =& $headers[$key];
}

In the above example we simple loop through each line, and get the key and value. We also try to detect whether the line doesn't belong to the previous header. It isn't foolproof, but it works in most cases.

All that's left now is to parse the message, and that takes only one line of code:

$message = implode('', $message);

It simply joins together all the separate lines into one variable.

Finally, we need to return the headers and the message, like this:

// Return array
$email = array();
$email['message'] = $message;
$email['headers'] = $headers;

return $email;

Now that we've parsed all the data, we can actually do something with it, for example:

$email = parse_email ($email);

echo 'From: ' . htmlentities($email['headers']['From']) . '';
echo 'Subject: ' . htmlentities($email['headers']['Subject']) . '';
echo nl2br(htmlentities($email['message']));
echo '


';

If you put the above code in the for-loop (when getting the e-mails) a list of all the e-mails will be displayed.

Please note that the parse_email() function isn't complete yet, and it doesn't handle e-mail attachments (or multi-part e-mails). However, this goes beyond the scope of this tutorial, so you will have to write attachment handling yourself or find another tutorial.

Conclusion

In this tutorial I've shown you how to read e-mail from a POP3 inbox using a POP3 class. Most of the examples in this tutorial were fairly simple, and we haven't really done anything exciting, but there are plenty of interesting things you can do, like a web-mail interface, an e-mail-to-weblog script, an automated support helpdesk, an e-mail bot, and much more.

Click here to download the complete POP3 script

If you have any questions, feel free to drop 'em below, or join us at PHPit Forums for more PHP discussion!

« Previous: Basics Continued & Parsing the e-mail



7 Responses to “Read your e-mail with PHP!”

  1. Marc Charles Says:

    You may want to clean up the code examples…you have missing ending ‘ that are going to end up causing some errors.

  2. ramzi Says:

    I am very happy, now. I have been looking for the article like this and I got this excellent tutorial. Thank you…Thank you…Thank you…

  3. Priya George Says:

    The International PHP Magazine carries a news article based on this tutorial called ‘Use PHP to Read Mail’, “Dennis Pallett shows you how to read POP3 e-mail with PHP and a POP3 library over at PHPit.net. He explains that it is possible to use PHP to read your e-mail instead of Microsoft Outlook or Mozilla Thunderbird. Dennis walks you through the necessary steps involved in reading your e-mail with PHP, and shows you how to display new e-mails in your inbox.

    Dennis starts with the basics. He shows you how to retrieve e-mails from your inbox using POP3 protocol, and the raw PHP socket functions, such as fsockopen. Further, he shows how to parse e-mails by separating the e-mail headers from the message by using …”

    [READ ON]

  4. John Says:

    http://phpmailer.sourceforge.net/

  5. greyedemon Says:

    SMTP and POP3 are old protocols so simple that they can be easily operated by human from telnet. Slightly more complicated is sending attachments, but because the sender can choose way how to send attachment (encoding, …) it is na so complicated. The hardest problem is processing incoming emails from a generic email client - what about this? Do you have a script which can handle it??? If yes, please let me know !

  6. speed Says:

    Connected to mail server POP3 login() - Error: No connection avalible

    I am getting this error?

    Any one can advise me?

  7. 448191 Says:

    ># speed Says:
    >August 14th, 2006 at 5:31 pm
    >
    >Connected to mail server POP3 login() - Error: No connection avalible
    >
    >I am getting this error?
    >
    >Any one can advise me?

    If you’re on a server without root access, you can’t use any port below 1024.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Basics
  2. Basics Continued & Parsing the e-mail
  3. Parsing the e-mail continued & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file