PHP
  Home arrow PHP arrow Page 4 - Building the Index Page for a PHP Email Application
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Building the Index Page for a PHP Email Application
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-11-08


    Table of Contents:
  • Building the Index Page for a PHP Email Application
  • Navigation and Headers
  • Retrieving Messages
  • Attachments

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building the Index Page for a PHP Email Application - Attachments
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek