Home arrow PHP arrow Page 4 - Building A PHP-Based Mail Client (part 3)

Coming Forward - PHP

This third (and final) segment of our case study discusses theprocess of sending MIME-encoded email, demonstrating how to compose,forward and reply to email through a Web browser. It also discusses, indetail, the process of constructing a MIME-encoded message withattachments, explains PHP's HTTP upload capabilities, and examines thestandard error handler used throughout this case study.

TABLE OF CONTENTS:
  1. Building A PHP-Based Mail Client (part 3)
  2. Composing Yourself
  3. Return To Sender
  4. Coming Forward
  5. Setting Boundaries
  6. Under Construction
  7. When Things Go Wrong...
  8. Game Over
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 8
January 23, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

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: