Building A PHP-Based Mail Client (part 3) - Return To Sender (
Page 3 of 8 )
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%"> </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"> </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: