Home arrow PHP arrow Page 4 - Building the Index Page for a PHP Email Application

Attachments - PHP

In this third part of a four-part article series on building a PHP email application, we will look at the index page. This page is the heart of the application. We will also look at how to handle attachments in a message and how to integrate them into this application.

TABLE OF CONTENTS:
  1. Building the Index Page for a PHP Email Application
  2. Navigation and Headers
  3. Retrieving Messages
  4. Attachments
By: Leidago
Rating: starstarstarstarstar / 8
November 08, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

An email message often has a couple of parts in it. I will not go into what they all are, but if there is more than one part, then there is a good chance that the message contains at least one attachment. Then all we have to do is download the attachment(s). 

To find an attachment in an email message, first determine the number of parts in the message. If the message contains more than one part, start to search for the attachment and then save it to file when you find it:

<?
//fetch the message parts
$msgparts=imap_fetchstructure($stream,$mID);
   /*
Find the part type, remember that Text is the first type in the
body type table:
0 Text  
1 Multipart
etc
So we are looking for non text type...
*/
   if ($msgparts->type!=0){ 
       if ($msgparts->encoding==3){
$msgparts=base64_decode($msgparts);
 }
/*You need to search two objects here, both return a "attribute" and "value" properties in the arrays:
dparameters
parameters.
The attribute and value properties will contain the file name of the attachments if any.
*/       
 //find the filename in dparameters object if any..     
       if ($msgparts->dparameters>0){
           foreach ($msgparts ->dparameters as $dparam){
            if ($dparam->attribute==

There are two things that you should do if you choose to implement attachment handling in this application. First you need to create a table that will take the name of the attachment (in the form of a link), the messageID of the message tow which the attachment belongs, and finally the user id. You create a separate table for attachments because one message can have more than one attachment, otherwise you would just put the attachment name in the messages table.

Second, you need to add the attachment code just before you insert the newly downloaded messages in the database. That is also the point at which you should insert the attachment data.

delete.php -  The delete page just removes a message from the database and the returns the user to the index page. To delete only the message that we want, we use the msg_id that is passed by the index page:

<?
if(isset($_GET['mid'])){
$query="DELETE from messages WHERE msg_id='".$_GET['mid']."'";
mysql_query($query);
header("location:index.php");
}
?>

view.php -  The view page enables you to read the entire email message that is retrieved from the database. You are presented with a table that shows all the headers and the message body. You also have the opportunity to reply to the message. In addition, the page includes all the application folders and navigation that you get on the index page:

First, a dynamic table is built to display the various message headers and body  retrieved from the database:

 if($n){
                         while($r = mysql_fetch_array($re)){
                         ?>
        <tr>
          <td width="10%" class="view">From</td>
          <td width="90%" class="td"><?=$r['from']; ?></td>
        </tr>
        <tr>
          <td class="view">To</td>
          <td class="td"><? echo  $email; ?></td>
        </tr>
        <tr>
          <td class="view">Subject</td>
          <td bgcolor="#FFFFFF" class="td"><?=$r['subject']; ?
></td>
        </tr>
        <tr>
          <td class="view">Date</td>
          <td bgcolor="#FFFFFF" class="td"><?=$r['msg_date']; ?
></td>
        </tr>
        <tr>
          <td colspan="2" ><br>
                         <? echo "".$r['msg_body'].""; ?>
                          <br>
                          <br /></td>
          </tr>

A link is presented here that takes you to the reply page:

        <tr>
          <td colspan="2" align="center"><br /><a
href="reply.php?mid=<?=$r['msg_id']; ?>&uid=<?=$user?>"
class="logout">Reply to this Message</a><br /></td>
          </tr>
                        <? } 
                        }?>
      </table> 

When looking at the message headers on the index page, you will see a link that points you to the view page; that link includes a mid variable. The script below uses that variable to retrieve the full message from the database:

<?php
            session_start();
            include "connect.php";
            $query_email = "SELECT email,user_id FROM user WHERE
user_id = '".$_SESSION['userid']."'";
            $result_email= mysql_query($query_email);
            $num_email= mysql_num_rows($result_email);
            if($num_email > 0 ){
            while($res = mysql_fetch_array($result_email)){
            $email = $res['email']; 
            $user = $res['user_id'];
}
            }else{
            echo mysql_error();
}          

The actual message is retrieved here and the result of the query is held in the $n variable. This variable is used to create a dynamic table later on:  

            $query = "select * from messages where msg_id =
'".$_GET['mid']. "'";
            $re = mysql_query($query);
            if(!$re){
            echo mysql_error();
}
            $n = mysql_num_rows($re);
            $c= 1;

Once the message has been viewed, it will be classed as an old message, so we need to update the database to reflect this. The checked column in the messages table needs to be set to one, so that the next time you visit the index page it won't retrieve this message as a new message:

//set checked flag to true
            $query_upd = "UPDATE messages SET checked = '1' WHERE
msg_id = '".$_GET['mid']. "'"; 
            $res = mysql_query($query_upd);
?>

Conclusion

In this part we looked at the index page, which is at the heart of the application and from which you can view or delete individual messages. We also had a brief look at how to handle attachments in a message and how to integrate them into this application. The issue of attachments is avoided in most PHP email tutorials on the Internet, so I hope the code that I put together here will be helpful. The next article will look at the pages that are referenced by the navigation links.



 
 
>>> More PHP Articles          >>> More By Leidago
 

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: