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:
$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:
// Split header and message
$header = array();
$message = array();
$is_header = true;
foreach ($email as $line) {
if ($line == '
if ($line == '
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
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:
July 13th, 2006 at 7:57 pm
You may want to clean up the code examples…you have missing ending ‘ that are going to end up causing some errors.
July 14th, 2006 at 10:43 am
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…
July 14th, 2006 at 3:04 pm
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]
July 15th, 2006 at 6:23 am
http://phpmailer.sourceforge.net/
July 18th, 2006 at 9:47 pm
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 !
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?
August 20th, 2006 at 10:57 pm
># 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.