HTML
  Home arrow HTML arrow Page 5 - 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 
Sun Developer Network 
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: 5 stars5 stars5 stars5 stars5 stars / 12
    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 - Listing the application's source code, second section


    (Page 5 of 5 )

    Having listed the (X)HTML file, which runs the corresponding client-side code, here are the pair of PHP files that send email and process contact data, along with a sample contact XML file:

    <?php
    // ***************************************************
    // sendmail.php file - sends email
    //
    //****************************************************
    // 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();
    }
    ?>

    <?php
    // ***************************************************
    // addcontact.php file – adds new contact to XML file
    //
    //****************************************************
    $file='contacts.xml';
    $str='<contact>'."\n".'<name>'.$_POST
    ['fullname'].'</name>'."\n".'<email>'.$_POST
    ['email'].'</email>'."\n".'</contact>'."\n".'</contactlist>';
    if(!$fp=fopen($file,'r+')){
                trigger_error('Error opening contacts
    file',E_USER_ERROR);
    }
    $contents=fread($fp,filesize($file));
    rewind($fp);
    $contents=str_replace('</contactlist>',$str,$contents);
    //header('Content-Type: text/xml');
    fwrite($fp,$contents);
    fclose($fp);
    // sample contact XML file
    ?>

    <?xml version="1.0" encoding="iso-8859-1"?>
    <contactlist>
    <contact>
    <name>Full Name 1</name>
    <email>address1@domain1.com</email>
    </contact>
    <contact>
    <name> Full Name 2</name>
    <email> address2@domain2.com </email>
    </contact>
    <contact>
    <name> Full Name 3</name>
    <email> address3@domain3.com </email>
    </contact>
    <contact>
    <name> Full Name 4</name>
    <email> address4@domain4.com </email>
    </contact>
    <contact>
    <name> Full Name 5</name>
    <email> address5@domain5.com </email>
    </contact>
    </contactlist>

    Bottom line

    After some hard work, this is the end of the series. Over these tutorials, I went through the development of an AJAX-driven email client, by progressively building the JavaScript/(X)HTML layers, as well as coding the respective PHP files that interact with the server. Hopefully, this series has illustrated the particulars of working with multiple http requester objects across a single application, in conjunction with extending a little more the boundaries of your background in AJAX. Now you that have the knowledge and the willpower, start creating AJAX-based applications. It’s really fun!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    HTML ARTICLES

    - Using a 3D HTML Table as a Recordset
    - Building a 3D HTML Table
    - Maximizing and Restoring HTML Images: Layer ...
    - Completing Construction of a Database Form w...
    - Maximizing and Restoring Images in a Tabular...
    - Building the Recordset for an HTML Database ...
    - Laying Out a Database Form with HTML
    - Tabular Database Form Functions with HTML
    - Tabular Database Forms with HTML
    - Using the Find Functions for HTML Database F...
    - Sorting for Database Forms with HTML
    - Edit and Other Database Form Functions with ...
    - More Database Form Functions with HTML
    - Database Form Functions with HTML
    - Using the HTML Table Element as a Recordset






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