Read your e-mail with PHP!
(Page 3 out of 3)The only problem is that some headers use multiple lines, and we'll have to account for that. The following code does the trick:
$headers = array();
foreach ($header as $line) {
$colon_pos = strpos($line, ':');
$space_pos = strpos($line, ' ');
if ($colon_pos === false OR $space_pos < $colon_pos) {
// attach to previous
$previous .= "\r\n" . $line;
continue;
}
// Get key
$key = substr($line, 0, $colon_pos);
// Get value
$value = substr($line, $colon_pos+2);
$headers[$key] = $value;
$previous =& $headers[$key];
}
In the above example we simple loop through each line, and get the key and value. We also try to detect whether the line doesn't belong to the previous header. It isn't foolproof, but it works in most cases.
All that's left now is to parse the message, and that takes only one line of code:
It simply joins together all the separate lines into one variable.
Finally, we need to return the headers and the message, like this:
$email = array();
$email['message'] = $message;
$email['headers'] = $headers;
return $email;
Now that we've parsed all the data, we can actually do something with it, for example:
echo 'From: ' . htmlentities($email['headers']['From']) . '';
echo 'Subject: ' . htmlentities($email['headers']['Subject']) . '';
echo nl2br(htmlentities($email['message']));
echo '
';
If you put the above code in the for-loop (when getting the e-mails) a list of all the e-mails will be displayed.
Please note that the parse_email() function isn't complete yet, and it doesn't handle e-mail attachments (or multi-part e-mails). However, this goes beyond the scope of this tutorial, so you will have to write attachment handling yourself or find another tutorial.
Conclusion
In this tutorial I've shown you how to read e-mail from a POP3 inbox using a POP3 class. Most of the examples in this tutorial were fairly simple, and we haven't really done anything exciting, but there are plenty of interesting things you can do, like a web-mail interface, an e-mail-to-weblog script, an automated support helpdesk, an e-mail bot, and much more.
Click here to download the complete POP3 script
If you have any questions, feel free to drop 'em below, or join us at PHPit Forums for more PHP discussion!
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.