PHP Helps you read your email
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:
<?php
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@address.goes.here.com, '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 ‘<pre>’;
print_r ($email);
echo ‘</pre>’;
}
If you run the above example, it will print every new e-mail in your inbox, including the headers of each e-mail.
Source: Totally PHP
Comments
Write a comment
You must be logged in to post a comment.

Comment from Day care jobs Directory
Time: April 22, 2008, 1:33 pm
We have a functionality in Outlook “send receive” so it will automatically pick the email from server to our inbox, can we customize this functionality in PHP? If this can be done, it would be really great.