Coding Folders for a PHP Email Application

In this article, the fourth and final one of our series covering the creation of a PHP email application, we are going to look at the code for some of the remaining pages of the mail application. Chief among these is the NewMsg.php page, which is where items, to be more precise, new messages, are either saved as drafts or saved as sent messages.

Let’s see how this one script affects the other ones. Here’s an illustrative diagram:

NewMsg.php – > Sent message

         |____________________

         |                                       |

Drafts Folder                       Sent Message FolderOptional

Let’s start with the NewMsg page. First you are presented with a form that will take the following input: To, Cc, BCc, Subject, Attachment and message text. All of these are required to send a valid email message.

<table width="100%" border="0" class="block">

        <tr>

          <td width="10%" valign="top"><strong>To</strong></td>

          <td width="90%"><input name="to" type="text" id="to"
size="70"  value="<? if(isset($r['to'])){

                          $r['to'];} ?>"/></td>

        </tr>

        <tr>

          <td valign="top"><strong>Cc</strong></td>

          <td><input name="cc" type="text" id="cc" size="70"
value="<? if(isset($r['cc'])){

                          $r['cc'];} ?>"/></td>

        </tr>

        <tr>

          <td valign="top"><strong>Bcc</strong></td>

          <td><input name="bcc" type="text" id="bcc" size="70"
value="<? if(isset($r['bcc'])){

                          $r['bcc'];} ?>"/></td>

        </tr>

        <tr>

          <td valign="top"><strong>Subject</strong></td>

          <td><input name="sub" type="text" id="sub" size="80"  value="<? if(isset($r['subject'])){

                          $r['subject'];} ?>"/></td>

        </tr>

        <tr>

          <td valign="top"><strong>Attachment</strong></td>

          <td>

                          <input name="userfile" type="file"
id="userfile" size="80" value="<? if(isset($r['attachment'])){

                          $r['attachment'];} ?>"/></td>

        </tr>

        <tr>

          <td valign="top"><strong>Message:</strong></td>

          <td><? $checked = false;

                          if($checked){ ?>

                          <textarea name="msg" cols="90" rows="9"
id="msg"><? if(isset($r['msg_body'])){

                          $r['msg_body'];} } ?></textarea>

                          <? if($checked){

// Automatically calculates the editor base path based on the
_samples directory.

// This is useful only for these samples. A real application
should use something like this:

// $oFCKeditor->BasePath = ‘/FCKeditor/’ ;   // ‘/FCKeditor/’ is
the default value.

$sBasePath =’fckeditor/fckeditor.php';

$oFCKeditor = new FCKeditor(‘FCKeditor1′) ;

$oFCKeditor->BasePath         = $sBasePath ;

$oFCKeditor->Value               = ‘This is some <strong>sample
text</strong>. You are using <a
href="http://www.fckeditor.net/">FCKeditor</a>.’ ;

$oFCKeditor->Create() ;

}

?>

                          </td>

        </tr>

        <tr>

          <td>&nbsp;</td><*/span>

          <td><input type="submit" name="Submit" value="Send
Message" />

            <input name="draft" type="submit" id="draft"
value="Save As Draft" />

            <input name="save" type="checkbox" id="save"
value="checkbox" />

            Save Sent Message</td>

        </tr>

      </table>

The text in red shows a selection of choices. The first choice is to just send the message as is. The second gives you the option of  saving the message as a draft, and the third choice gives you the option of saving the message in the "Sent Messages" Folder. Some of these choices can be made simultaneously and others cannot. For example, if you click on "save as draft" then the message will be saved in the drafts folder instead of being sent. On the other hand, if you want to send the message and save it in the sent folder, that is allowed.

{mospagebreak title=Form Data}

That is all there is to the form. Next we take a look at the code that will handle the form data:

<?php

                        session_start();

                        include "connect.php";

                        include("fckeditor/fckeditor.php");

A

//retrieve msg from sent folder            

            if(isset($_GET['sid'])){

            $query_msgs = "select * from sent WHERE sent_id =
‘".$_GET['sid']."’";

            $re = mysql_query($query_msgs);

            $n = mysql_num_rows($re);

            $r = mysql_fetch_array($re);

            }

//retrieve msg from drafts folder         

            if(isset($_GET['did'])){

            $query_msgs = "select * from drafts WHERE draft_id =
‘".$_GET['did']."’";

            $re = mysql_query($query_msgs);

            $n = mysql_num_rows($re);

            $r = mysql_fetch_array($re);

            }

           if(isset($_GET['tid'])){

            $query_msgs = "select * from trash WHERE trash_id =
‘".$_GET['did']."’";

            $re = mysql_query($query_msgs);

            $n = mysql_num_rows($re);

            $r = mysql_fetch_array($re);

            }

B

           

//is form submitted

if(isset($_POST['Submit'])){

//get login stuff

$query_email = "SELECT * 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'];

            $remuser = $res['remuser'];

            $rempass = $res['rempass'];

            $smtp = $res['smtp'];

}

            }else{

            echo mysql_error();

}                                  

include("lib/phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();                                      // set mailer to use SMTP

$mail->Host = $smtp;  // specify main and backup server

$mail->SMTPAuth = true;     // turn on SMTP authentication

$mail->Username = $remuser;  // SMTP username

$mail->Password = $rempass; // SMTP password

//check that the headers are filled

$from=$_POST['from'];

$cc=$_POST['cc'];

$bcc=$_POST['bcc'];

$to=$_POST['to'];

$subject=$_POST['sub'];

$msg=$_POST['msg'];

//checkbox

if($_POST['save'] !== ""){

$query_sent = "INSERT INTO sent SET to = ‘$to’, from =
‘$from’,subject = ‘$subject’,";

            $query_sent .= "msg_body = ‘$msg’, userid =
‘".$_SESSION['userid']."’,cc =’".$_POST['']."’,";      

            $query_sent .="bcc =’".$_POST['bcc']."’,date_sent
=NOW(),attachment = ‘".$_POST['userfile']."’";

            mysql_query($query_sent);

}

// put headers in mail object

$mail->From = $from;

$mail->AddAddress($to);

$mail->AddCC($cc);

$mail->AddBCC($bcc);

$mail->Subject = $subject;

$mail->Body    = $msg;

$mail->AltBody = $msg;

$mail->AddReplyTo($from);

$mailer->ConfirmReadingTo = $from;

if($_FILES['userfile']['name'] !== ""){

$mailer->AddAttachment($_FILES['userfile']['tmp_name'], $_FILES
['userfile']['name']);

}

if(!$mail->Send())

{

  

   $msg2 =$mail->ErrorInfo;

   header("location:resultpage.php?msg=1&error=$msg2");

   exit;

}else{

$msg2="Reply sent to $to";

header("location:resultpage.php?msg=2&res=$to");

exit;

}

}

C

//save as draft

if(isset($_POST['draft'])){

$query_draft = "INSERT INTO drafts SET to = ‘$to’, from =
‘$from’,subject = ‘$subject’,";

            $query_draft .= "msg_body = ‘$msg’, userid =
‘".$_SESSION['userid']."’,cc =’".$_POST['cc']."’,"; 

            $query_draft .="bcc =’".$_POST['bcc']."’,attachment =
‘".$_POST['userfile']."’";   

            if(mysql_query($query_draft)){

            $msg2="Reply sent to $to";

header("location:resultpage.php?msg=2&res=$to");

exit;

            };

}

?>

The NewMsg.php page will be displayed whenever you click on the Sent, Trash and Draft links that are displayed on all the pages in the application. When you do click on these links, they send a variable to the NewMsg page. The "Sent" link sends a variable called "sid," the "Trash" link sends a variable called "tid" and the "Draft" link sends a variable called "did." Section A of the code above handles these links when it receives those variables. When any of these variables are received, none of the rest of the code is executed.

{mospagebreak title=Explaining Sections B and C}

Section B handles the sending of email messages. This is where we will use the freely available PHP Mailer class mentioned in the first article. The first thing it does is to retrieve the user connection details:

$query_email = "SELECT * 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'];

            $remuser = $res['remuser'];

            $rempass = $res['rempass'];

            $smtp = $res['smtp'];

}

These details are needed to connect to the mail server.  Then we instantiate and fill the PHPMailer class with the user’s connection details:

$mail = new PHPMailer();

$mail->IsSMTP();                                      // set
mailer to use SMTP

$mail->Host = $smtp;  // specify main and backup server

$mail->SMTPAuth = true;     // turn on SMTP authentication

$mail->Username = $remuser;  // SMTP username

$mail->Password = $rempass; // SMTP password

//check that the headers are filled

$from=$_POST['from'];

$cc=$_POST['cc'];

$bcc=$_POST['bcc'];

$to=$_POST['to'];

$subject=$_POST['sub'];

$msg=$_POST['msg'];

Here, we check to see whether the user wants the message to be saved to the "Sent" folder. If so, we insert the message details in to the sent table:

//checkbox

if($_POST['save'] !== ""){

$query_sent = "INSERT INTO sent SET to = ‘$to’, from = ‘$from’,subject = ‘$subject’,";

                $query_sent .= "msg_body = ‘$msg’, userid = ‘".$_SESSION['userid']."’,cc =’".$_POST['']."’,";   

                $query_sent .="bcc =’".$_POST['bcc']."’,date_sent
=NOW(),attachment = ‘".$_POST['userfile']."’";

                mysql_query($query_sent);

}

Now we add the message details to the PHPMailer class. I highly recommend that you take a look at the links I provided in the first article to learn more about the PHPMailer class:

// put headers in mail object

$mail->From = $from;

$mail->AddAddress($to);

$mail->AddCC($cc);

$mail->AddBCC($bcc);

$mail->Subject = $subject;

$mail->Body    = $msg;

$mail->AltBody = $msg;

$mail->AddReplyTo($from);

$mailer->ConfirmReadingTo = $from;

Another important aspect I think needs highlighting is the attachment. Here you can see  how I get the file into the class by using part of PHP’s upload code:

if($_FILES['userfile']['name'] !== ""){

$mailer->AddAttachment($_FILES['userfile']['tmp_name'], $_FILES
['userfile']['name']);

}

Finally, we send the message off, using the Send() function. This function is a wrapper for PHP’s mail() function. We also make a provision for errors. If any errors occur the user is sent to the results page, which is used to display error messages:

if(!$mail->Send())

{

   $msg2 =$mail->ErrorInfo;

   header("location:resultpage.php?msg=1&error=$msg2");

   exit;

}else{

$msg2="Reply sent to $to";

header("location:resultpage.php?msg=2&res=$to");

exit;

}

}

Section C deals with what happens when the "Save As Drafts" button is pressed. The code starts by checking whether that button is pressed, and if so, it inserts the message details into the drafts table. On successful insertion the user is redirected to the results page.

{mospagebreak title=The Sent, Trash and Drafts Folders}

The above folders have similar code, so I’m not going to list the code of each of them here. Let me explain what happens when you want to view the items inside a folder. A query string variable called userID is received, which is used to retrieve messages related to that user. Then a query is run on the table of the related folder (can be the sent, trash or drafts table). Based on the results of that query, a dynamic table is created and all the messages are listed. Here’s the code for the Sent folder:

<?

                include "connect.php";

//get all msgs

                $query_msgs = "select * from sent WHERE userid =
‘".$_SESSION['userid']."’";

                $re = mysql_query($query_msgs);

                $n = mysql_num_rows($re);

                if($n > 0){

                while($r = mysql_fetch_array($re))

{

?>

<tr class="td">

          <td valign="top" class="list">

                                <a href="NewMsg.php?sid=<?=$r
['sent_id'];?>" class="email"><?=$r['to'];?></a>

                                </td>

                                 <td valign="top" class="list"><?
=$r['from']; ?></td>

          <td valign="top" class="list"><?=$r['subject']; ?></td>

        </tr>

<?

}

}

?>

Conclusion

That’s it! You should now have a fully functional PHP email client. I’ve used very general code to make it easy for you to adapt it to your liking. The basics are covered, so any changes you make to this will just make your client more useful. In the NewMsgs page I’ve used a text editor called fckEditor because it makes it possible to format the text, and in so doing enables you to sent HTML email messages. You can download this editor from the Internet, it is entirely free. Also, if you do not want to use this editor, you can use the normal textarea form element instead.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye