Building the Index Page for a PHP Email Application - Navigation and Headers (
Page 2 of 4 )
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:
<tr>
<td class="table"><a href="index.php?uid=<?=$_SESSION
['userid'];?>" class="menu">Inbox</a>(<? if(isset($numEmails)){
echo "($numEmails)";} ?>)</td>
</tr>
<tr>
<td class="table"><a href="sent.php" class="menu">Sent
(<? $query_sent = "select * from sent WHERE userid = '".$_SESSION
['userid']."'";
$re = mysql_query($query_sent);
$num = mysql_num_rows($re);
if($num > 0){echo $num;}else{echo 0;}?>)</a></td>
</tr>
<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>
</tr>
<tr>
<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>
</tr>
</table>
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"
class="listtop">Subject</td>
<td width="22%" valign="top" class="listtop">Date</td>
<td width="11%" valign="top"
class="listtop">Action</td>
</tr>
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("
",$date);
$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'";
mysql_query($query);
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.