picture of Mark Musone
Don't you hate it when you are at a friend's house or visiting relatives and you want to check your email but dont know your POP/IMAP settings. Or even worse, they dont have your POP or IMAP software?
Web based email seems to be the talk of the Net lately. Heres how to use PHP to make a quick, simple and effective IMAP or POP mail reader. Once this is done, you can now be able to read your email from anywhere in the world with only a web browser.
The first necessity is to make sure that you have an available IMAP server. you can get the UW imap server and libraries from ftp://ftp.cac.washington.edu/imap/ (you will need the libraries to compile into php anyway, so you might as well install the IMAP server that comes with it also)
Once you have a running IMAP server and PHP compiled with IMAP support (make sure you read the PHP docs on how to compile PHP with imap), the rest is easy!
We'll make a nice and simple 3-script based mail reader.
The first script will be to read in your IMAP username and password. We will use the standard PHP authentication for that.
login.php3 contains:

<?php

if (!$PHP_AUTH_USER) {
    
Header("WWW-authenticate: basic realm=\"Mail Chek\"");
    
Header("HTTP/1.0 401 Unauthorized");
} else {
    
$MYDIR=ereg_replace("/[^/]+$","",$PHP_SELF);
    
Header("Location: $SERVER_NAME$MYDIR/messages.php3");
}

?>
This simply reads in your username and password and redirects you to the read mail reading page.
messages.php3 contains:

<?php

$MAILSERVER
="{localhost/imap}";
$link=imap_open($MAILSERVER,$PHP_AUTH_USER,$PHP_AUTH_PW);
$headers=imap_headers($link);

for(
$x=1$x count($headers); $x++) {
    
$idx=($x-1);
    echo 
"<a href=\"view.php3?num=$x\">$headers[$idx]</a><br>";
}

?>
It opens up an IMAP connection to the mailserver specified by $MAILSERVER, passing in your username and password.
It then get a list of all the messgae headers and in a loop, prints them all out. Besides printing them out, it also makes each one a link to view.php3, passing it that message number.
view.php3 contains:

<?php

$MAILSERVER
="{localhost/imap}";
$link=imap_open($MAILSERVER,$PHP_AUTH_USER,$PHP_AUTH_PW);
$header=imap_header($link,$num);

echo 
"From: $header[fromaddress]<br>";
echo 
"To: $header[toaddress]<br>";
echo 
"Date: $header[Date]<br>";
echo 
"Subject: $header[Subject]<br><br>";
echo 
imap_body($link,$num);

?>
view.php3 opens up the IMAP connection the same as above, and gets the mail message's header information and prints it out.
It then reads in the body of the mail message and prints that out to the screen.
Thats it! You now have a functional web based mail reader...Hotmail watch out, here comes php! ;^)
--Mark