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.
Next: Getting the server-side application layer completed: listing the “addcontact.php” PHP file >>
More HTML Articles
More By Alejandro Gervasio