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

Navigation and Headers - 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.

  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



Next the navigation menu is created and the Inbox, Sent, Trash and Drafts page links are added, all of which will list the number of items contained within them:

          <td class="table"><a href="index.php?uid=<?=$_SESSION
['userid'];?>" class="menu">Inbox</a>(<? if(isset($numEmails)){
                          echo "($numEmails)";} ?>)</td>
          <td class="table"><a href="sent.php" class="menu">Sent
(<? $query_sent = "select * from sent WHERE userid = '".$_SESSION
            $re = mysql_query($query_sent);
            $num = mysql_num_rows($re);
            if($num > 0){echo $num;}else{echo 0;}?>)</a></td>
        <tr class="table">
          <td class="menu"><a href="drafts.php"
class="menu">Drafts(<? $query_msgs = "select * from drafts WHERE
userid = '".$_SESSION['userid']."'";
            $res= mysql_query($query_msgs);
            $n = mysql_num_rows($res);
            if($n > 0){
            echo $n;}else{echo 0;}?>)</a></td>
          <td class="table"><a href="trash.php"
class="menu">Trash(<? $query_trash = "select * from trash WHERE
userid = '".$_SESSION['userid']."'";
            $result = mysql_query($query_trash);
            $num_trash = mysql_num_rows($result);
            if($num_trash > 0){
            echo $n_trash;}else{echo 0;}?>)</a></td>

Next, a table is created to display the column headers in which the message headers will be displayed. These headers are From, Subject, Date and Action:

                  <table width="100%" border="0" cellpadding="0"
cellspacing="1" class="block">
        <tr class="table">
        <td width="7%" class="listtop">Status</td>
          <td width="22%" valign="top" class="listtop">From</td>
          <td width="38%" valign="top"
          <td width="22%" valign="top" class="listtop">Date</td>
          <td width="11%" valign="top"

The code below shows how the actual messages are downloaded from the mail server and inserted directly into the database:

<? //if there are new msgs, get them
            if(isset($numEmails) &&($numEmails > 0)) {
            for($i = 1; $i < $numEmails+1; $i++)
            $mailHeader = @imap_headerinfo($stream, $i);
            $from = $mailHeader->fromaddress;
            $subject = strip_tags($mailHeader->subject);
            $msgNo = imap_msgno( $stream, $i);
            $date = $mailHeader->date;
            list($dayName,$day,$month,$year,$time) = split("
                        $time = substr($time,0,5);
            $date = $day ."- ". $month ."- ". $year . " ". $time;
            $dates = $year ."- ". $month ."- ". $day;
If you are going to implement attachment handling, this will be a
good place to place your code. It will also be a good place to
place your insert query code. See the attachments section below
for a fuller explanation.
//put messages in db
            include "connect.php";
            $query = "INSERT INTO messages SET msg_num =
'$msgNo', from = '$from',subject = '$subject',";
            $query .= "msg_body = '$msg_body', checked =
'0',msg_date = '$dates', userid = '$user'";

When the newly downloaded messages are inserted in to the database, the checked column in the messages table is set to zero(0). This makes it easy to differentiate between old and new messages. You can also use the date to differentiate between old and new messages.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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