Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Building A PHP-Based Mail Client (part 2)
  2. A Picture Is Worth A Thousand Words
  3. The Way Things Work
  4. Structure And Syntax
  5. Room With A View
  6. Getting Down
  7. Miles To Go
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 12
January 11, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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!

Here's what it looks like:



 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: