JavaScript
  Home arrow JavaScript arrow Page 4 - Sending Email with an SMTP Client Built wi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Sending Email with an SMTP Client Built with Prototype and PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-10-02

    Table of Contents:
  • Sending Email with an SMTP Client Built with Prototype and PHP
  • The complete client-side code of the SMTP application
  • Sending email with PHP
  • Full source code of the SMTP client application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Sending Email with an SMTP Client Built with Prototype and PHP - Full source code of the SMTP client application


    (Page 4 of 4 )

    As I promised in the section that you just read, below I listed the full source code corresponding to this Prototype-based SMTP application, so you can copy it and pasted it into your preferred text editor for further modifications and improvements.

    Here is the corresponding code listing:

    (signature of prototype_mail.htm file)


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />

    <title>Prototype-based SMTP Client</title>

    <style type="text/css">

      body{

       padding: 0;

       margin: 0;

       background: #fff;

     }

     h1{

        font: bold 24px Arial, Helvetica, sans-serif;

         color: #000;

        text-align: center;

     }

     p{

        font: bold 12px Arial, Helvetica, sans-serif;

         color: #000;

         padding-right: 100px;

        text-align: right;

     }

      textarea{

        width: 400px;

         height: 300px;

         padding: 2px;

         font: normal 12px Arial, Helvetica, sans-serif;

          color: #000;

     }

     #maincontainer{

        width: 600px;

          height: 550px;

          margin-left: auto;

          margin-right: auto;

        background: #f5ebb1;

        border: 1px solid #000;

     }

      #state{

          font: bold 12px Arial, Helvetica, sans-serif;

           color: #00f;

         padding: 10px 0 10px 10px;

     }

     .textbox{

        width: 400px;

        padding: 2px;

          font: normal 12px Arial, Helvetica, sans-serif;

           color: #000;

     }

     .formbutton{

        font: normal 12px Arial, Helvetica, sans-serif;

        color: #000;

     }

    </style>

    <script language="javascript" src="prototype-1.4.0.js"></script>

    <script language="javascript">

     // attach handler to window object

      Event.observe(window,'load',initializeEmailClient,false);

     // initialize email application

      function initializeEmailClient(){

        Event.observe
    ($('resetbutton'),'click',clearMessageField,false);

        Event.observe('mailform','submit',sendEmail);

     }

     // reset 'message' field to send email

       function clearMessageField(){

        Field.clear($('message'));

     }

     // send http request

       function sendEmail(e){

     var params='to='+$F('to')+'&subject='+$F('subject')+'&cc='+$F
    ('cc')+'&bcc='+$F('bcc')+'&message='+escape($F('message'));

     var xmlobj=new Ajax.Updater('state','send_email.php',{method:
    'get',parameters: params});

     // prevent form from submitting

        Event.stop(e);

     }

    </script>

    </head>

    <body>

    <h1>Prototype-based SMTP Client</h1>

    <div id="maincontainer">

    <div id="state">STATUS: COMPOSING MESSAGE...</div>

    <form id="mailform">

    <p>To: <input type="text" id="to" title="enter email address of
    recipient" class="textbox" /></p>

    <p>Subject: <input type="text" id="subject" title="enter the
    subject of your message" class="textbox" /></p>

    <p>Cc: <input type="text" id="cc" class="textbox" /></p>

    <p>Bcc: <input type="text" id="bcc" class="textbox" /></p>

    <p><textarea name="message" title="Type here the text of your
    message" id="message"></textarea></p>

    <p><input type="submit" value="Send Message" class="formbutton"
    title="Send Message"/>

    <input type="reset" value="Clear All Fields" class="formbutton"
    title="Clear All Fields" />

    <input type="button" value="Clear Message" class="formbutton"
    title="Clear Message" id="resetbutton"/></p>

    </form>

    </div>

    </body>

    </html>

    (signature of send_mail.php file)

    <?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 you'll realize, only two files are required to get this SMTP client up and running, asides from the Prototype source files, of course. However, given the decent functionality provided by the application, it's really worthwhile to give it a try.

    Final thoughts

    During this three-part series, I provided you with a step-by-step guide on how to build an expansible SMTP client, which uses the robust functionality provided by the Prototype JavaScript library to send email messages to different recipients.

    Hopefully, after reading this group of articles, you'll have a better idea of how to take advantage of the numerous AJAX capacities that come bundled with this useful software package.

    See you in the next web development tutorial!


    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.

       · In this concrete article of the series, the PHP script that actually sends email to...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT