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. |