Sending Email with an SMTP Client Built with Prototype and PHP - Sending email with PHP
(Page 3 of 4 )
In accordance with the concepts that I deployed in the previous section, I'm going build a short PHP script which will be tasked with receiving the email data inputted by the user via the corresponding front-end, and then sending out the email message to the specified recipients.
As you can see, the logic that stands behind this PHP snippet sounds really easy to grasp, but you'll find it even simpler if you take a look at its signature, which is listed below:
<?php
// clean up GET data
array_map('trim',$_GET);
// check if 'to' field has been filled or not
if(!$_GET['to']){
echo 'STATUS: PLEASE SPECIFY AN EMAIL ADDRESS';
exit();
}
// check if 'subject' field has been filled or not
if(!$_GET['subject']){
echo 'STATUS: PLEASE SPECIFY A SUBJECT';
exit();
}
// check if 'message' field has been filled or not
if(!$_GET['message']){
echo 'STATUS: PLEASE ENTER YOUR MESSAGE';
exit();
}
// get message fields
$to=$_GET['to'];
$subject=$_GET['subject'];
$message=$_GET['message'];
// define MIME headers
$headers="MIME-Version 1.0rn"."Content-Type: text/plain;
charset=iso-8859-1rn"."From: myaddress@mydomain.comrn"."Reply-to:
myaddress@mydomain.comrn";
// check if 'Cc' field has been filled or not
if(!empty($_GET['cc'])){
$headers.="Cc: ".$_GET['cc']."rn";
}
// check if 'Bcc' field has been filled or not
if(!empty($_GET['bcc'])){
$headers.="Bcc: ".$_GET['bcc']."rn";
}
// send email
if(!@mail($to,$subject,$message,$headers)){
echo 'STATUS: ERROR SENDING MESSAGE';
exit();
}
else{
echo 'STATUS: MESSAGE WAS SENT SUCCESSFULLY';
exit();
}
?>
As illustrated above, the PHP script responsible for sending out email messages is in fact quite simple to follow. It begins by checking some required parameters, such as the email address of the recipient to which the message will be submitted, and the subject and text of the message as well.
After performing these crucial verification tasks, the script finally determines if any values have been specified for the respective carbon copies and blind carbon copies, and finally sends the email message by using the intuitive "mail()" PHP native function. In addition, in all cases, different status messages are displayed in the client according to the different data checking processes performed in the server.
Okay, at this point hopefully you'll have a pretty accurate idea of how this SMTP client works, since its respective client and server-side modules are indeed simple to understand. Thus, in the last section of this tutorial, I'm going to list the complete source code that corresponds to this email application, this time including the short PHP script that you learned before.
To see how this will be achieved, go ahead and read the next few lines.
Next: Full source code of the SMTP client application >>
More JavaScript Articles
More By Alejandro Gervasio