Rio Bautista’s TechLog

Tech solutions worth remembering

Accessing a POP3 mailbox using PHP

I was tasked to write a simple PHP process that should download email from a given POP3 account and process the content.

First thing that comes to my mind is to use a POP3 library from PHP Classes which I did. It seemed simple enough though I had to make a few tweaks to get it running. Here’s a short tutorial of what I did:

  1. Download the POP3 class library from PHP Classes;
  2. Copy the PHP class to your source directory. You may want to rename it to follow your convention;
  3. In your client PHP program you’ll of course need to add reference to this library:


    <?
    require_once(“POP3.class.php5.inc”);

    ?>

  4. Now to start a POP3 session, you’ll need to create a POP3 instance and issue a connect command:

    <?
    require_once(“POP3.class.php5.inc”);
    $pop3 = new POP3();
    $pop3->connect(‘pop3.yourmailserver.com’);

    ?>

  5. However, this did not work on my server. By default, this POP3 class attempts to use sockets and APOP and that doesn’t seem to work on my POP3 server. But that doesn’t mean it won’t work with your server. Now, you can pass 3 parameters to the constructor to change these options:

    <?
    require_once(“POP3.class.php5.inc”);
    $pop3 = new POP3(false,’testlog.txt’, false);
    $pop3->connect(‘pop3.yourmailserver.com’);

    ?>

  6. Now you’re ready to login:

    <?
    require_once(“POP3.class.php5.inc”);
    $pop3 = new POP3(false,’testlog.txt’, false);
    $pop3->connect(‘pop3.yourmailserver.com’);
    $pop3->login(‘username‘,’password‘, false);

    ?>
  7. Note that I added a third parameter to explicitly say that I’m not using APOP (which again doesn’t work on my POP server);
  8. Let’s try checking for new mail:

    <?
    require_once(“POP3.class.php5.inc”);
    $pop3 = new POP3(false,’testlog.txt’, false);
    $pop3->connect(‘pop3.yourmailserver.com’);
    $pop3->login(‘username’,’password’, false);

    $status = $pop3->getOfficeStatus();
    if ($status == false) {
        die(‘Error retrieving mailbox status’);
    }
    $mail_count = $status[‘count’];
    ?>

  9. getOfficeStatus returns an associative array status of your mailbox. Why not do a print_r to see what other information can be retrieved from the $status array;
  10. Finally we’ll extract each email from the box:

    <?
    require_once(“POP3.class.php5.inc”);
    $pop3 = new POP3(false,’testlog.txt’, false);
    $pop3->connect(‘pop3.yourmailserver.com’);
    $pop3->login(‘username’,’password’, false);

    $status = $pop3->getOfficeStatus();
    if ($status == false) {
        die(‘Error retrieving mailbox status’);
    }
    $mail_count = $status[‘count’];
    for ($i = 1; $i <= $mail_count; $i++) {
        $email = $pop3->getMails(array($i));
        list($id, $message) = each($email);
    }
    ?>

  11. You might wonder why I had to put $i in a array, that’s because getMails require an array of message numbers to extract and also returns an array of extracted messages indexed by their message numbers. This is why we used list and each to extract the message from the $email array (don’t expect $email[0] to contain the message).

There, I hope this explains enough to let you use the POP3 class available in PHP Classes without going through the troubles I went through. What’s left to do now to complete your mail client is to parse each message you retrieve. I’ll probably post that next.

Advertisements

January 8, 2008 - Posted by | email, getMails, php, pop3

3 Comments »

  1. hey i tried but getting

    Fatal error: Cannot access self:: when no class scope is active in

    Comment by gary | October 29, 2012 | Reply

    • please post a snippet of your code so we can try to take a look.

      Comment by Rio | October 31, 2012 | Reply

  2. nice

    Comment by sero | October 30, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: