HomePHP 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.
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:
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.
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.
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 -->