HomePHP Page 6 - Building A PHP-Based Mail Client (part 2)
Getting Down - PHP
Now that you've got a basic mail reader up and running, it's timeto learn a little more about attachments. This second segment analyzesMIME-encoded attachments, demonstrating how to decode and download them,and then integrates attachment handling features into the primitive mailclient previously demonstrated.
You've already seen how every attachment is linked to the script "download.php" via a message and part number. This "download.php" script is fairly interesting - its job is to decode the selected attachment from its plaintext form back into binary data and then allow the user to download it to the local workstation.
<?
// download.php - download attachment
// includes and session check
// check for required values
if (!$id || !$pid)
{
header("Location: error.php?ec=4");
exit;
}
// form not submitted
if(!$submit)
{
?>
<html>
<head>
</head>
<body bgcolor="White">
<?
// page header
?>
<font face="Verdana" size="-1">
<form action="<? echo $PHP_SELF?>" method="post">
<input type="hidden" name="id" value="<? echo $id; ?>">
<input type="hidden" name="pid" value="<? echo $pid; ?>">
<input type="submit" name="submit" value="Click here"><font face="Verdana"
size="-1"> to begin downloading the selected attachment to your local
workstation.</font>
</form>
<font face="Verdana" size="-1">Once the attachment has completed
downloading, you may <a href="view.php?id=<? echo $id; ?>">return to the
selected message</a> or <a href="list.php">go back to the message
listing</a>.</font>
<?
}
else
{
// open POP connection
$inbox = @imap_open ("{". $SESSION_MAIL_HOST . "/pop3:110}",
$SESSION_USER_NAME, $SESSION_USER_PASS) or header("Location:
error.php?ec=3");
// parse message structure
$structure = imap_fetchstructure($inbox, $id);
$sections = parse($structure);
// look for specified part
for($x=0; $x<sizeof($sections); $x++)
{
if($sections[$x]["pid"] == $pid)
{
$type = $sections[$x]["type"];
$encoding = $sections[$x]["encoding"];
$filename = $sections[$x]["name"];
}
}
$attachment = imap_fetchbody($inbox, $id, $pid);
// send headers to browser to initiate file download
header ("Content-Type: $type");
header ("Content-Disposition: attachment; filename=$filename");
if ($encoding == "base64")
{
// decode and send
echo imap_base64($attachment);
}
else
{
// add handlers for other encodings here
echo $attachment;
}
// clean up
imap_close($inbox);
}
?>
After a few basic error checks, the script produces some
simple instructions - click a button to initiate file download, or click a link to go back to the main message listing. In this case, the button is actually a form (more on this later), which, once submitted, connects to the POP3 server, selects the specified message, retrieves the message part specified by $pid, decodes it using PHP's built-in BASE64 decoder, and sends HTTP headers to the browser to prepare it for a file download (whew!). Once the browser receives the headers, it should pop up a "Save As" dialog box, allowing the user to save the file to his or her local workstation, where it can be modified and edited.
Note that the filename sent in the "Content-Disposition: " header is the original name of the file.
I'll draw your attention here to the very cool imap_fetchbody() function (not to be confused with imap_body(), which you saw in the previous script), which can be used to retrieve a specific section of a message body, given the part number. This section, once retrieved, usually consists of what looks like gibberish, but is actually text-encoded binary data; it needs to be converted back into its binary representation using an appropriate decoding mechanism. The script above only knows how to handle BASE64 encoding - feel free to add other support for other encoding methods also.
You'll notice also that I've used a form to call the script which actually initiates the download. My original stab at this was to simply call the script and pass it the message and part IDs via the URL GET method - for example, "download.php?id=13&pid=4". However, while this technique worked without a problem in Netscape and lynx browsers, and even in version 5.0 of Internet Explorer, I noticed a problem with Internet Explorer 5.5; the browser chokes if asked to download a script containing GET-type parameters. Consequently, I decided to use a form and pass parameters via the POST method instead.
Some users have also reported another strange problem with Internet Explorer 5.5 - rather than downloading the target file, the browser has a nasty tendency to download the calling script instead. I plan to look into this at some point - if you have any ideas on what this is all about, let me know!