Home arrow HTML arrow Page 2 - Sending Email with AJAX: Interacting with the Server
HTML

Sending Email with AJAX: Interacting with the Server


Here we are again. Welcome to the last tutorial of the series “Sending email with AJAX.” As you may have guessed regarding the title, this three-part series explains the development of a fairly simple AJAX-driven email client application, which offers some interesting capabilities for sending email, as well as for displaying and adding contacts, all without the need to involve page reloads.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 23
January 31, 2006
TABLE OF CONTENTS:
  1. · Sending Email with AJAX: Interacting with the Server
  2. · Sending email with PHP: developing a straightforward script
  3. · Getting the server-side application layer completed: listing the “addcontact.php” PHP file
  4. · Putting the layers together: listing the application’s source code, first section
  5. · Listing the application's source code, second section

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
HTML ARTICLES

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials