PHP
  Home arrow PHP arrow Page 4 - Building A PHP-Based Mail Client (part 3)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Building A PHP-Based Mail Client (part 3)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2002-01-23


    Table of Contents:
  • Building A PHP-Based Mail Client (part 3)
  • Composing Yourself
  • Return To Sender
  • Coming Forward
  • Setting Boundaries
  • Under Construction
  • When Things Go Wrong...
  • Game Over

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 3) - Coming Forward
    ( Page 4 of 8 )

    The third of this merry trio is "forward.php", which also receives a message ID from "view.php"; it uses this message ID to determine which message has been selected for forwarding.

    Of the three forms, "forward.php" has perhaps the most work to do. The form generated by "compose.php" is almost completely empty, while that generated by "reply.php" has only to worry about importing the correct headers and message body from the original message. The "forward.php" script, though, has to perform all the functions of "reply.php" and also handle attachments that may be embedded in the original message.

    Consequently, the code for "forward.php" is a hybrid what you've already seen in "reply.php" and "view.php" - take a look:

    <? // forward.php - forward message // includes and session check // check for missing values if (!$id) { header("Location: error.php?ec=4"); exit; } $inbox = @imap_open ("{". $SESSION_MAIL_HOST . "/pop3:110}", $SESSION_USER_NAME, $SESSION_USER_PASS) or header("Location: error.php?ec=3"); ?> <html> <head> </head> <body bgcolor="White"> <? // page header // get message headers and structure $headers = imap_header($inbox, $id); $structure = imap_fetchstructure($inbox, $id); // if multipart mail, parse if(sizeof($structure->parts) > 1) { $sections = parse($structure); $attachments = get_attachments($sections); } ?> <table width="100%" border="0" cellspacing="3" cellpadding="5"> <tr> <td width="100%">&nbsp;</td> <td valign=top align=center><a href="compose.php"><img src="images/compose.gif" width=50 height=50 alt="" border="0"><br><font face="Verdana" size="-2">Compose</font></a></td> <td valign=top align=center><a href="list.php"><img src="images/list.gif" width=50 height=50 alt="" border="0"><br><font face="Verdana" size="-2">Messages </font></a></td> <td valign=top align=center><a href="javascript:document.forms[0].submit()"><img src="images/send.gif" width=50 height=50 alt="" border="0"><br><font face="Verdana" size="-2">Send!</font></a></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="#C70D11"><font size="-1">&nbsp;</font></td> </tr> </table> <table border="0" cellspacing="1" cellpadding="5" width="100%"> <form action="send.php" method="POST" enctype="multipart/form-data"> <tr> <td valign=top><font face="Verdana" size="-1"><b>From: </b></font></td> <td valign=top width=100%><input type="Text" name="from" size="30" maxlength="75" value="<? echo $SESSION_USER_NAME . "@" . $SESSION_MAIL_HOST; ?>"> </td> </tr> <tr> <td valign=top><font face="Verdana" size="-1"><b>To: </b></font></td> <td valign=top><input type="Text" name="to" size="30"></td> </tr> <tr> <td valign=top><font face="Verdana" size="-1"><b>Cc: </b></font></td> <td valign=top><input type="Text" name="cc" size="30"></td> </tr> <tr> <td valign=top><font face="Verdana" size="-1"><b>Bcc: </b></font></td> <td valign=top><input type="Text" name="bcc" size="30"></td> </tr> <? // empty subject correction if ($headers->Subject == "") { $subject = "No subject"; } else { $subject = $headers->Subject; } ?> <tr> <td valign=top><font face="Verdana" size="-1"><b>Subject: </b></font></td> <td valign=top><input type="Text" name="subject" size="50" value="<? echo "Fw: " . $subject; ?>"></td> </tr> <tr> <td valign=top><font face="Verdana" size="-1"><b>Message: </b></font></td> <td valign=top bgcolor="White"> <textarea name="body" cols="60" rows="15" wrap="VIRTUAL" > <? // display message body with forward symbol > echo "\n\n"; echo ">From: $headers->fromaddress\n"; echo ">To: $headers->toaddress\n"; if($headers->ccaddress) { echo ">Cc: $headers->ccaddress\n"; } echo ">Date: $headers->Date\n"; echo ">Subject: $headers->Subject\n"; // if multipart, display text parts if(is_array($sections)) { for($x=0; $x<sizeof($sections); $x++) { //if(substr($sections[$x]["type"], 0, 4) == "text") if($sections[$x]["type"] == "text/plain" && $sections[$x]["disposition"] != "attachment") { echo str_replace("\n", "\n>", htmlspecialchars(trim(imap_fetchbody($inbox, $id, $sections[$x]["pid"])))); } } } else { // else display body echo str_replace("\n", "\n>", htmlspecialchars(trim(imap_body($inbox, $id)))); } ?> </textarea></td> </tr> <tr> <td valign=top><font face="Verdana" size="-1"><b>Attachments: </b></font></td> <td valign=top bgcolor="White"><input type="file" name="attachment" size="20"> <? // if attachments exist // display as list of checkboxes if (is_array($attachments)) { ?> <br> <font face="Verdana" size="-1"><ul> <? for($x=0; $x<sizeof($attachments); $x++) { echo "<input type=checkbox checked name=amsg[] value=" . $attachments[$x]["pid"] . ">" . $attachments[$x]["name"] . " (" . ceil($attachments[$x]["size"]/1024) . " KB)<br>"; } ?> </ul></font> <? } ?> </td> </tr> <input type="hidden" name="id" value="<? echo $id; ?>"> </form> </table> </body> </html> <? // clean up imap_close($inbox); ?>
    As is routine by now, the first part of the script checks for a valid session and then opens up a connection to the user's mailbox on the POP3 server. The supplied message ID is then used to retrieve the structure and headers for the specified message, and parse() it for attachments.

    <? $inbox = @imap_open ("{". $SESSION_MAIL_HOST . "/pop3:110}", $SESSION_USER_NAME, $SESSION_USER_PASS) or header("Location: error.php?ec=3"); // get message headers and structure $headers = imap_header($inbox, $id); $structure = imap_fetchstructure($inbox, $id); // if multipart mail, parse if(sizeof($structure->parts) > 1) { $sections = parse($structure); $attachments = get_attachments($sections); } ?>
    Unlike "reply.php", which has to read the original message's headers and pre-fill the form's recipient and subject fields appropriately, "forward.php" has to display these headers within the message body itself.

    <? // display message body with forward symbol > echo "\n\n"; echo ">From: $headers->fromaddress\n"; echo ">To: $headers->toaddress\n"; if($headers->ccaddress) { echo ">Cc: $headers->ccaddress\n"; } echo ">Date: $headers->Date\n"; echo ">Subject: $headers->Subject\n"; ?>
    Next, the same code seen previously in "reply.php" is used to isolate and print the text sections of the message.

    <? // if multipart, display text parts if(is_array($sections)) { for($x=0; $x<sizeof($sections); $x++) { //if(substr($sections[$x]["type"], 0, 4) == "text") if($sections[$x]["type"] == "text/plain" && $sections[$x]["disposition"] != "attachment") { echo str_replace("\n", "\n>", htmlspecialchars(trim(imap_fetchbody($inbox, $id, $sections[$x]["pid"])))); } } } else { // else display body echo str_replace("\n", "\n>", htmlspecialchars(trim(imap_body($inbox, $id)))); } ?>
    A list of the original message's attachments are also displayed, together with checkboxes which allow the user to select which ones should get forwarded.

    <? // if attachments exist // display as list of checkboxes if (is_array($attachments)) { ?> <br> <font face="Verdana" size="-1"><ul> <? for($x=0; $x<sizeof($attachments); $x++) { echo "<input type=checkbox checked name=amsg[] value=" . $attachments[$x]["pid"] . ">" . $attachments[$x]["name"] . " (" . ceil($attachments[$x]["size"]/1024) . " KB)<br>"; } ?>
    Pay attention to the checkboxes - when the form is submitted, the array $amsg will contain the part IDs of the attachments selected for inclusion in the forwarded message. I'll be using this array extensively in the next script.

    And, just to make things interesting, how about also allowing the user to upload and add a new attachment to the forwarded message?

    <form action="send.php" method="POST" enctype="multipart/form-data"> <!-- snipped out HTML --> <input type="file" name="attachment" size="20"> <!-- snipped out HTML -->
    Here's what it all looks like:



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

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek