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 2 out of 3)

We are almost finished with our basic e-mail script, but there's still one problem. After retrieving e-mails it doesn't mark them as 'read', and the next time you will retrieve the same e-mails. Obviously you don't want to receive the same e-mails again and again, so we need to do something about this.

The solution lies in the delete_mail() method, which is used to delete the e-mail from the mail server. You will also save disk space, since the e-mail no longer exists on your mail server. See the example below:

// Remove from mail server
$do = $pop3->delete_mail ($i);
if ($do == false) {
        echo $pop3->error;
}

If you are building a web-mail interface, you probably don't want to delete the e-mail, because you'll be unable to get the new e-mail in your regular e-mail application (e.g. Outlook, Thunderbird).

We've now built a very simple e-mail interface, but it's not really useable yet. For one, we can't display any details of the e-mail yet, because it's all mashed together, so let's do something about that.

Parsing the e-mail

The first thing we'll have to do is separate the e-mail headers from the message. The following code will do just that:

function parse_email ($email) {
        // Split header and message
        $header = array();
        $message = array();

        $is_header = true;
        foreach ($email as $line) {
                if ($line == '

' . "\r\n") continue;
                if ($line == ' ' . "\r\n") continue;
                if ($line == ' ' . "\r\n") continue;
                if ($line == '
' . "\r\n") { $is_header = false; continue; }

                if ($is_header == true) {
                        $header[] = $line;
                } else {
                        $message[] = $line;
                }
        }

        // TODO: parse headers and message

        return $email;
}

As you can see in the above example, we use the

and delimiters, which get added by the POP3 library, to separate the headers and message.

Now that we've split the headers and message, we will need to parse all the headers, since we want each header separate, so we can display the Subject, From, To, etc. This is fairly easy, since all the headers look like this:

Key: value goes here

« Previous: Introduction & Basics
Next: Parsing the e-mail continued & Conclusion »



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