PHP
  Home arrow PHP arrow Page 6 - Building A PHP-Based Mail Client (part...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building A PHP-Based Mail Client (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2002-01-11

    Table of Contents:
  • Building A PHP-Based Mail Client (part 2)
  • A Picture Is Worth A Thousand Words
  • The Way Things Work
  • Structure And Syntax
  • Room With A View
  • Getting Down
  • Miles To Go

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building A PHP-Based Mail Client (part 2) - Getting Down


    (Page 6 of 7 )

    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


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway