Home arrow JavaScript arrow Page 3 - Sending Email with an SMTP Client Built with Prototype and PHP
JAVASCRIPT

Sending Email with an SMTP Client Built with Prototype and PHP


If you're a web developer who wants to acquire a solid background in building applications with the popular Prototype JavaScript library, then this set of articles may be what you're looking for. Welcome to the last installment of the series that began with "Building an SMTP Client with Prototype." Made up of three friendly tutorials, this series teaches you how to use this powerful software package to create a highly expansible email application that works with most modern browsers.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
October 02, 2007
TABLE OF CONTENTS:
  1. · Sending Email with an SMTP Client Built with Prototype and PHP
  2. · The complete client-side code of the SMTP application
  3. · Sending email with PHP
  4. · Full source code of the SMTP client application

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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