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