PHP
  Home arrow PHP arrow Page 3 - Coding Folders for a PHP Email Application
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Coding Folders for a PHP Email Application
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek