Viewing Email in PHP

This is just a quick script that I mostly stole from Wrox Press's PHP Programming (a fine book, you should buy it), so that I could check my inbox while away from my computer. It's not very sophisticated, and i'll probably improve it sometime, but it's adequate for the moment.

Firstly, create a link to the server:

<?PHP
$user="username";
$pass="password";
$mailServer="{pop3.servername.net/pop3}INBOX";
$link=imap_open($mailServer,$user,$pass);
echo(imap_last_error());
?>

Get the list of headers from your inbox, and output it complete with links passing in the message variable via query string for viewing:

<ul>
<?PHP
$headers=imap_headers($link);
for($x=1;$x<=count($headers); $x++)
{
$idx=$x-1;
echo("<li><a href=\"list.php?message=$x\">".$headers[$idx]."</a></li>");
}
?>
</ul>

If a message is being asked for ($message), retrieve the header and body, and output preformatted.

You'll need to use $_GET["message"] rather than just $message in newer versions of PHP

<hr><pre>
<?PHP
if($message)
{
echo(imap_fetchheader($link,$message));
echo("<br>");
echo(imap_body($link,$message));
}
?>
</pre></hr>