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

Return To Sender - 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
Next up, "reply.php", which receives a message ID from "view.php"; it uses this message ID to determine which message has been selected for replying. Stripped to its essence, this is an HTML form similar to the one you just saw, with some additional code to format the message and its headers appropriately.

<? // reply.php - reply to message // includes and session checks // check for required values if (!$id) { header("Location: error.php?ec=4"); exit; } // open POP connection $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 for specified message $headers = imap_header($inbox, $id); // check for Reply-To: header and use if available if($headers->reply_toaddress) { $to = trim($headers->reply_toaddress); } else { $to = trim($headers->fromaddress); } // check for Re: in subject header if(strtolower(substr(trim($headers->Subject), 0, 3)) == "re:") { $subject = $headers->Subject; } else { $subject = "Re: " . $headers->Subject; } // get message structure and parse $structure = imap_fetchstructure($inbox, $id); 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" value="<? echo $to; ?>"></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> <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 $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" > <? // attribution line echo "\n\n\nOn " . $headers->Date . ", you wrote: \n>"; // iterate through message parts if(is_array($sections)) { for($x=0; $x<sizeof($sections); $x++) { // if text type, display 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 { echo str_replace("\n", "\n>", htmlspecialchars(trim(imap_body($inbox, $id)))); } ?> </textarea></td> </tr> <tr> <td valign=top><font face="Verdana" size="-1"><b>Attachment: </b></font></td> <td valign=top bgcolor="White"><input type="file" name="attachment" size="20"></td> </tr> </form> </table> </body> </html> <? // clean up imap_close($inbox); ?>
This is a little more complicated than "compose.php", since I need to first retrieve the original message from the mail server and then pre-fill the form with values sourced from that message. For example, I need to fill the To: field with the email address of the message's original sender (or the contents of the Reply-To: header, if it exists),

<? // check for Reply-To: header and use if available if($headers->reply_toaddress) { $to = trim($headers->reply_toaddress); } else { $to = trim($headers->fromaddress); } ?>
insert the subject line from the original message with a "Re: " prefix (if one doesn't already exist),

<? // check for Re: in subject header if(strtolower(substr(trim($headers->Subject), 0, 3)) == "re:") { $subject = $headers->Subject; } else { $subject = "Re: " . $headers->Subject; } ?>
add an attribution line,

<? // attribution line echo "\n\n\nOn " . $headers->Date . ", you wrote: \n>"; ?>
and quote the text within the message body.

<? echo str_replace("\n", "\n>", htmlspecialchars(trim(imap_body($inbox, $id)))); ?>
Here, too, I need to run the parse() function to look for attachments, and only display text attachments within the message body (this code snippet was previously explained in the second segment of this article).

<? // get message structure and parse $structure = imap_fetchstructure($inbox, $id); if(sizeof($structure->parts) > 1) { $sections = parse($structure); $attachments = get_attachments($sections); } // iterate through message parts if(is_array($sections)) { for($x=0; $x<sizeof($sections); $x++) { // if text type, display 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 { echo str_replace("\n", "\n>", htmlspecialchars(trim(imap_body($inbox, $id)))); } ?>
Finally, since users should have the ability to add attachments to a reply, the form also includes a "file" input type and a "multipart/form-data" form encoding type.

<form action="send.php" method="POST" enctype="multipart/form-data"> <!-- snipped out HTML --> <input type="file" name="attachment" size="20"> <!-- snipped out HTML --> </form>
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: