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

Abstract

In this tutorial you will learn how to read POP3 e-mail with PHP and a POP3 library.

Introduction

E-mail is something we use every day, and almost everyone has their own e-mail address. To read our e-mail most of us tend to use something like Microsoft Outlook or Mozilla Thunderbird, but it's also possible to use PHP to read your e-mail. In this tutorial you will learn how.

I will take you through all the steps necessary to read your e-mail with PHP, and show you how to display all the newest e-mails in your inbox.

Let's get started!

The Basics

To retrieve the e-mails in our inbox we will have to use the POP3 protocol, and the raw PHP socket functions, such as fsockopen. But why should we all write it from scratch, when there are already many great libraries available?

That's why we're going to use a POP3 class from PHPClasses.org, available at http://www.phpclasses.org/browse/package/1120.html. Unfortunately, you'll need to login to download the package, but registration doesn't take long.

Once you've downloaded the class, try the following example:


require ('pop3.class.inc');

$pop3 = new POP3;

// Connect to mail server
$do = $pop3->connect ('YOUR-EMAIL-SERVER-HERE);
if ($do == false) {
        die($pop3->error);
}

echo 'Connected to mail server';

$pop3->close();

?>

In the above example you will have to put in your own mail server. If you don't know what your own mail server is, try something like mail.YOURDOMAIN.com or mail.YOURISP.com.

If everything goes okay, you will see 'Connected to mail server' when running the example. This means that the script was able to connect to your mail server, and the next step is to login to your e-mail inbox.

This can be done with the login() method, and you will have to pass your username (which is usually your e-mail address) and your password, like this:

// Login to your inbox
$do = $pop3->login ('[email protected], 'password-here');

if ($do == false) {
        die($pop3->error);
}

echo 'Logged in';

Now that we've logged into our inbox, we're almost ready to start downloading new e-mail messages, but first we have to get the 'office status', which basically tells our script how many new e-mails there are.

The office status can be retrieved with the get_office_status() method; see the example below:

// Get office status
$status = $pop3->get_office_status();

if ($status == false) {
        die($pop3->error);
}

$count = $status['count_mails'];

echo 'There are ' . $count . ' new e-mails waiting for you!';

As you can see in the above example, the get_office_status() method returns an array, which includes the number of new e-mails, but also a few other details, such as the total size of the new e-mails. This will allow you to create a neat looking progress bar.

Now we can start retrieving the new e-mails with a for-loop and the get_mail() method, like this:

for ($i = 1; $i <= $count; $i++) {
        $email = $pop3->get_mail($i);

        if ($email == false) {
                echo $pop3->error;
                continue;
        }

        echo '

';
        print_r ($email);
        echo '
';
}

If you run the above example, it will print every new e-mail in your inbox, including the headers of each e-mail.

Next: 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