HTML
  Home arrow HTML arrow Page 2 - Sending Email with AJAX: Interacting with ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
HTML

Sending Email with AJAX: Interacting with the Server
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2006-01-31

    Table of Contents:
  • Sending Email with AJAX: Interacting with the Server
  • Sending email with PHP: developing a straightforward script
  • Getting the server-side application layer completed: listing the “addcontact.php” PHP file
  • Putting the layers together: listing the application’s source code, first section
  • Listing the application's source code, second section

  • 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


    Sending Email with AJAX: Interacting with the Server - Sending email with PHP: developing a straightforward script


    (Page 2 of 5 )

    Anyone who has spent some time programming with PHP knows that by far the easiest way to send email is by using the “mail()” function, and of course, this case won’t be the exception to that rule. Indeed, it’s also possible to send email by utilizing TCP sockets, which can give much more control over each relevant task involved in the whole process, but for meeting the requirements of the AJAX-based application in question, the “mail()” function is more than enough.

    Now that you know what approach I’m going to take regarding this crucial topic, have a look at the PHP file below, which sends out email messages:

    // clean up POST data
    array_map('trim',$_POST);
    // check if 'recipient' field has been filled or not
    if(!$_POST['to']||$_POST['to']=='TO: '){
        echo 'STATUS: PLEASE SPECIFY AN EMAIL ADDRESS';
        exit();
    }
    // check if 'subject' field has been filled or not
    if(!$_POST['subject']||$_POST['subject']=='SUBJECT: '){
        echo 'STATUS: PLEASE SPECIFY A SUBJECT';
        exit();
    }
    // check if 'message' field has been filled or not
    if(!$_POST['message']){
        echo 'STATUS: PLEASE ENTER YOUR MESSAGE';
        exit();
    }
    // get message fields
    $to=str_replace('TO:','',$_POST['to']);
    $subject=str_replace('SUBJECT:','',$_POST['subject']);
    $message=$_POST['message'];
    // define MIME headers
    $headers="MIME-Version 1.0\r\n"."Content-Type: text/plain;
    charset=iso-8859-1\r\n"."From:
    myaddress@mydomain.com\r\n"."Reply-to:
    myaddress@mydomain.com\r\n";
    // check if 'CC' field has been filled or not
    if(!empty($_POST['cc'])&&$_POST['cc']!='CC: '){
        $headers.="Cc: ".str_replace('CC:','',$_POST['cc'])."\r\n";
    }
    // check if 'Bcc' field has been filled or not
    if(!empty($_POST['bcc'])&&$_POST['bcc']!='BCC: '){
        $headers.="Bcc: ".str_replace('BCC:','',$_POST['bcc'])."\r\n";
    }
    // send email
    if(!mail($to,$subject,$message,$headers)){
        echo 'STATUS: ERROR SENDING MESSAGE';
        exit();
    }
    else{
        echo 'STATUS: MESSAGE WAS SENT SUCCESSFULLY';
        exit();
    }

    Admittedly, the script above is extremely easy to follow. As you’ll recall from my previous article, all the data included when sending the pertinent email message is submitted by a regular POST http request, so the script begins cleaning up the respective POST data. In this specific case, I’ve only used the “trim()” PHP built-in function for removing unwanted white space, but you may want to apply a thorough filter on the data, by stripping tags and removing single and double quotes, depending on the configuration of your ”php.ini” file.

    The next thing the above script does is sequentially check to see if the “To,” “Subject” and “Message” fields have been filled in, respectively. If any of them hadn’t been populated, the script sends back to the client an indicative error message and program execution is stopped. In addition, as you can see, the email address verification process is kept at a basic level, which returns me to the issue I discussed earlier, since there’s plenty of room to implement more complex routines for checking the validity of entered data. Regarding this checking process, here are the lines that verify whether the pertinent fields have been filled in or not:

    // check if 'recipient' field has been filled or not
    if(!$_POST['to']||$_POST['to']=='TO: '){
        echo 'STATUS: PLEASE SPECIFY AN EMAIL ADDRESS';
        exit();
    }
    // check if 'subject' field has been filled or not
    if(!$_POST['subject']||$_POST['subject']=='SUBJECT: '){
        echo 'STATUS: PLEASE SPECIFY A SUBJECT';
        exit();
    }
    // check if 'message' field has been filled or not
    if(!$_POST['message']){
        echo 'STATUS: PLEASE ENTER YOUR MESSAGE';
        exit();
    }

    Now, by continuing with the explanation of each task performed by the script, after having checked input data, it determines whether the corresponding “Cc” and “Bcc” fields have been specified. In that case, they’re appended to the string that composes the MIME headers of the message. This process is conveniently illustrated as follows:

    $headers="MIME-Version 1.0\r\n"."Content-Type: text/plain; charset=iso-8859-1
    \r\n"."From: myaddress@mydomain.com\r\n"."Reply-to:
    myaddress@mydomain.com\r\n";
    // check if 'CC' field has been filled or not
    if(!empty($_POST['cc'])&&$_POST['cc']!='CC: '){
        $headers.="Cc: ".str_replace('CC:','',$_POST['cc'])."\r\n";
    }
    // check if 'Bcc' field has been filled or not
    if(!empty($_POST['bcc'])&&$_POST['bcc']!='BCC: '){
        $headers.="Bcc: ".str_replace('BCC:','',$_POST['bcc'])."\r\n";
    }

    Finally, the email message is sent out, as shown below:

    // send email
    if(!mail($to,$subject,$message,$headers)){
        echo 'STATUS: ERROR SENDING MESSAGE';
        exit();
    }
    else{
        echo 'STATUS: MESSAGE WAS SENT SUCCESSFULLY';
        exit();

    Well, so far I have written a PHP script for sending email, which although simple, is quite useful for my specific purpose of providing the AJAX application with this ability. What’s next? Fortunately, that’s simple to answer. In the next section, I’ll be listing the source code for the PHP snippet that inserts new contacts into the respective XML file, so you can have a clear idea of how the complete server-side layer looks. Thus, clink the link below and keep reading.

    More HTML Articles
    More By Alejandro Gervasio


       · Having developed the client-side application layer of this AJAX-based email program...
     

    HTML ARTICLES

    - Comparing Browser Response to Active Client ...
    - Testing Browser Response to Active Client Pa...
    - Active Client Pages: Completing the Code for...
    - ACP and Browsers: Setting up an Example
    - How Browsers Respond to Active Client Pages
    - Completing a Tree with Active Client Pages
    - HTML Form Verification and ACP
    - Building an ACP Tree
    - Completing an ACP 3D HTML Table Image Gallery
    - Building an ACP 3D HTML Table Image Gallery
    - A Multiple Page Image Gallery with Active Cl...
    - Building an Image Gallery with Active Client...
    - Concluding a Menu for All Browsers
    - A Vertical Menu for All Browsers
    - Downloading Long HTML Pages with ACP







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