Home arrow PHP arrow Page 3 - Coding Folders for a PHP Email Application

Explaining Sections B and C - PHP

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.

TABLE OF CONTENTS:
  1. Coding Folders for a PHP Email Application
  2. Form Data
  3. Explaining Sections B and C
  4. The Sent, Trash and Drafts Folders
By: Leidago
Rating: starstarstarstarstar / 8
November 15, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: