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

Room With A View - PHP

Now that you've got a basic mail reader up and running, it's timeto learn a little more about attachments. This second segment analyzesMIME-encoded attachments, demonstrating how to decode and download them,and then integrates attachment handling features into the primitive mailclient previously demonstrated.

TABLE OF CONTENTS:
  1. Building A PHP-Based Mail Client (part 2)
  2. A Picture Is Worth A Thousand Words
  3. The Way Things Work
  4. Structure And Syntax
  5. Room With A View
  6. Getting Down
  7. Miles To Go
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 12
January 11, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The next step, obviously, is to write a script that displays the complete contents of a particular message. You'll remember from the first part of this article that this script is called "view.php", and that it's passed a message number via the URL GET method from "list.php".

I've delayed writing this script for a while, primarily because I wasn't too comfortable with attachment handling. Now that I have a method to handle attachments, I'm confident that it shouldn't give me too much trouble...and, as it turns out, it doesn't. Take a look:

<?php // view.php - display message // includes // session check // 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"> <? // get message headers and structure $headers = imap_header($inbox, $id); $structure = imap_fetchstructure($inbox, $id); // if multipart, parse if(sizeof($structure->parts) > 1) { $sections = parse($structure); $attachments = get_attachments($sections); } ?> <table width="100%" border="0" cellspacing="3" cellpadding="5"> <!-- command buttons --> <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="reply.php?id=<? echo $id; ?>"><img src="images/reply.gif" width=50 height=50 alt="" border="0"><br><font face="Verdana" size="-2">Reply</font></a></td> <td valign=top align=center><a href="forward.php?id=<? echo $id; ?>"><img src="images/forward.gif" width=50 height=50 alt="" border="0"><br><font face="Verdana" size="-2">Forward</font></a></td> <td valign=top align=center><a href="delete.php?dmsg[]=<? echo $id; ?>"><img src="images/delete.gif" width=50 height=50 alt="" border="0"><br><font face="Verdana" size="-2">Delete</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> </tr> </table> <table border="0" cellspacing="1" cellpadding="5" width="100%"> <tr> <td valign=top><font face="Verdana" size="-1"><b>From: </b></font></td> <td valign=top width=100%><font face="Verdana" size="-1"><? echo htmlspecialchars($headers->fromaddress); ?></font></td> </tr> <tr> <td valign=top><font face="Verdana" size="-1"><b>To:</b></font></td> <td valign=top><font face="Verdana" size="-1"><? echo htmlspecialchars($headers->toaddress); ?> </font> </td> </tr> <?php if($headers->ccaddress) { ?> <tr> <td valign=top><font face="Verdana" size="-1"><b>Cc: </b></font></td> <td valign=top><font face="Verdana" size="-1"><? echo htmlspecialchars($headers->ccaddress); ?> </tr> <? } ?> <tr> <td valign=top><font face="Verdana" size="-1"><b>Date: </b></font></td> <td valign=top><font face="Verdana" size="-1"><? echo $headers->Date;?></font></td> </tr> <tr> <td valign=top><font face="Verdana" size="-1"><b>Subject: </b></font></td> <td valign=top><font face="Verdana" size="-1"> <? // correction for empty subject if ($headers->Subject) { echo $headers->Subject; } else { echo "No subject"; } ?> </font></td> </tr> <tr> <td colspan=2 valign="TOP"><pre><font face="Verdana"> <? // if multipart, display text sections if(is_array($sections)) { for($x=0; $x<sizeof($sections); $x++) { if(($sections[$x]["type"] == "text/plain" || $sections[$x]["type"] == "message/rfc822") && $sections[$x]["disposition"] != "attachment") { echo htmlspecialchars(stripslashes(trim(imap_fetchbody($inbox, $id, $sections[$x]["pid"])))); echo "<br>"; } } } else { echo htmlspecialchars(stripslashes(trim(imap_body($inbox, $id)))); } ?> </font></pre></td> </tr> <? // if attachments exist if (is_array($attachments)) { ?> <tr> <td valign=top><font face="Verdana" size="-1"><b>Attachments: </b></font></td> <td valign=top><font face="Verdana" size="-1"><ul> <? // display as list for($x=0; $x<sizeof($attachments); $x++) { echo "<li><a href=download.php?id=$id&pid=" . $attachments[$x]["pid"] . ">" . $attachments[$x]["name"] . " (" . ceil($attachments[$x]["size"]/1024) . " KB)</a>"; } ?> </ul></font></td> </tr> <? } ?> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="50%" align="left" valign="top"> <? if ($id > 1) { // display previous message link echo "<a href=view.php?id=" . ($id-1) ."><font face=Verdana size=-2>Previous message</font></a>"; } ?>&nbsp; </td> <td align="right" width="50%" valign="top"> <? // display next message link if ($id < $total) { echo "<a href=view.php?id=" . ($id+1) ."><font face=Verdana size=-2>Next message</font></a>"; } ?>&nbsp; </td> </tr> </table> </body> </html> <?php // clean up imap_close($inbox); ?>
This is a little complicated, so I'll explain it in detail. The first step - after performing the now-routine session and value checks - is to open a connection to the POP3 server

<? // open POP connection $inbox = @imap_open ("{". $SESSION_MAIL_HOST . "/pop3:110}", $SESSION_USER_NAME, $SESSION_USER_PASS) or header("Location: error.php?ec=3"); // get total messages // wondering why I need this? keep reading... $total = imap_num_msg($inbox); ?>
This connection is then used to retrieve the headers and structure of the specified message.

<? // get message headers and structure $headers = imap_header($inbox, $id); $structure = imap_fetchstructure($inbox, $id); ?>
Assuming that the message has more than one part, I run my custom parse() function on it.

<? // if multipart, parse if(sizeof($structure->parts) > 1) { $sections = parse($structure); $attachments = get_attachments($sections); } ?>
Much HTML code follows - this is very similar to the code used in "list.php" to display the message headers, so I'm not going to repeat the explanation here. The interesting part comes at the tail end, when it's time to display the body.

<tr> <td colspan=2 valign="TOP"><pre><font face="Verdana"> <? // if multipart, display text sections if(is_array($sections)) { for($x=0; $x<sizeof($sections); $x++) { if(($sections[$x]["type"] == "text/plain" || $sections[$x]["type"] == "message/rfc822") && $sections[$x]["disposition"] != "attachment") { echo htmlspecialchars(stripslashes(trim(imap_fetchbody($inbox, $id, $sections[$x]["pid"])))); echo "<br>"; } } } else { echo htmlspecialchars(stripslashes(trim(imap_body($inbox, $id)))); } ?> </font></pre></td> </tr>
In this case, I'm first checking the $sections variable returned by parse() to see if the message is, indeed, a multipart message. If it's not, I'm using the imap_body() function to display the complete body of the message (refer to the standard process flow a few pages back if this doesn't make sense to you). If, on the other hand, it does contain multiple parts, I'm iterating through the $sections array and displaying only those sections which *are not* attachments and *are* of type "text/plain". Note the huge array of string functions I'm using to sanitize the body prior to displaying it.

The last section of the script deals with attachments. In this case, my task is even simpler, thanks to the get_attachments() function - all I need to do is iterate through the array returned by get_attachments() and display the file names and sizes in a list.

<? // if attachments exist if (is_array($attachments)) { ?> <tr> <td valign=top><font face="Verdana" size="-1"><b>Attachments: </b></font></td> <td valign=top><font face="Verdana" size="-1"><ul> <? // display as list for($x=0; $x<sizeof($attachments); $x++) { echo "<li><a href=download.php?id=$id&pid=" . $attachments[$x]["pid"] . ">" . $attachments[$x]["name"] . " (" . ceil($attachments[$x]["size"]/1024) . " KB)</a>"; } ?> </ul></font></td> </tr> <? } ?>
Each attachment can be downloaded separately, by clicking on the link to "download.php". I'll be addressing this shortly - for the moment, note merely that "download.php" gets the message number ($id) and part number ($pid) as GET parameters.

This script needs to be linked to a bunch of other scripts as well. If you look at the command buttons at the top of the generated page, you'll see links to compose a new message and forward, delete or reply to the current message. Some of these scripts are also passed parameters via the GET method - take a brief look at them now and I'll come back to them later.

<!-- command buttons --> <!-- some HTML snipped out for readability --> <td><a href="compose.php">Compose</a></td> <td><a href="reply.php?id=<? echo $id; ?>">Reply</a></td> <td><a href="forward.php?id=<? echo $id; ?>">Forward</a></td> <td><a href="delete.php?dmsg[]=<? echo $id; ?>">Delete</a></td> <td><a href="list.php">Messages</a></td>
Note how I'm reusing "delete.php" to delete just one message, rather than a list, by passing it data in the format it expects. I *could* write a different version of "delete.php" just to delete a specific message...but I'm lazy, and this works just fine.

Finally - and this was actually an addition I made after a user interface review - it would be nice to have a way to move to the next or previous message from this screen itself, rather than going back to the list and selecting another message. And so, I've added a little code to the bottom of the page to display links to the previous and next message - they merely call "view.php" with a different message number.

<? if ($id > 1) { // display previous message link echo "<a href=view.php?id=" . ($id-1) ."><font face=Verdana size=-2>Previous message</font></a>"; } ?> <? // display next message link // now you know why I needed $total if ($id < $total) { echo "<a href=view.php?id=" . ($id+1) ."><font face=Verdana size=-2>Next message</font></a>"; } ?>
Here's what the finished product 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: