PHP
  Home arrow PHP arrow Page 3 - Coding Folders for a PHP Email Applica...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Coding Folders for a PHP Email Application
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2006-11-15

    Table of Contents:
  • Coding Folders for a PHP Email Application
  • Form Data
  • Explaining Sections B and C
  • The Sent, Trash and Drafts Folders

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Coding Folders for a PHP Email Application - Explaining Sections B and C


    (Page 3 of 4 )

    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


       · Hi Leidago, as I have commented before on the articles about the invoicing system, ...
       · Thank you for your critique, but i'm afraid We are just going into circles on the...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT