XML
  Home arrow XML arrow Page 2 - Developing the Server-side Layer of an Ema...
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 
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? 
XML

Developing the Server-side Layer of an Email Application in AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-04-12

    Table of Contents:
  • Developing the Server-side Layer of an Email Application in AJAX
  • Working with a POP3 server: defining the barebones of the "POP3Processor" class
  • Fetching email messages: defining the signature of the "fetch()" method
  • Assembling the POP3 client: putting client and server-side layers to work together

  • 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


    Developing the Server-side Layer of an Email Application in AJAX - Working with a POP3 server: defining the barebones of the "POP3Processor" class


    (Page 2 of 4 )

    Provided that all the JavaScript functions that I wrote in my previous tutorials were correctly understood, you can now turn your attention to the development of the PHP class. This class is charged with tasks related to connecting to the mail server, pushing raw POP3 commands, and fetching the list of email messages for further display.

    In order to tackle all these operations, the PHP class I plan to develop will expose only three core methods. The first one is the appropriate constructor, responsible for establishing a connection to a given mail server, in conjunction with the POP3 authentication commands. The second class method, "fetch()," as its name suggests, will transmit the required commands for fetching the formatted list of email messages from the corresponding inbox. Finally, the "close()" method will close the socket connection to the server, as one would expect from a regular server connection process.

    In accordance with the tasks described above, the barebones of the "POP3Processor" class can be defined like this:

    class POP3Processor{
        // connects to the POP3 server
        public function __construct(){
            // code for connecting to POP3 server goes here
        }
        // fetch email messages
        public function fetch(){
            // code for retrieving email messages goes here
        }
        // close mail server connection
        public function close(){
            // code for closing POP3 server connection goes here
        }
    }

    As you can see, the structure of the above class follows common sense rules, so it should be pretty easy to read and understand. Of course, this is only the skeleton of the class, which means we have to explicitly define each relevant class method. This is precisely what I'll do in the next few lines, so keep reading to figure out how the first method of the class will be defined.

    Connecting to the POP3 server: defining the "POP3Processor" class constructor method

    In order to handle all the operations related to connecting to the POP3 server, the class uses its constructor method. As you'll see in a few moments, this method accepts the usual input parameters for connecting to a specific server, that is, its name or IP address, and the username/password combination. Below is the signature for this method:

    public function __construct($host,$user,$password){
        if(!$this->fp=fsockopen($host,110,$errno,$errstr,30)){
            throw new Exception('Failed to connect to POP3 server
    '.$errstr.$errno);
        }
        stream_set_timeout($this->fp,2);
        $this->output.=fgets($this->fp,128).'<br />';
        fputs($this->fp,"USER $usern");// send USER command
        $this->output.=fgets($this->fp,128).'<br />';
        fputs($this->fp,"PASS $passwordn");// send PASS command
        $this->output.=fgets($this->fp,128).'<br />';
        $this->output.='||||';// send delimiter string
    }

    Well, if you study the code of the above method, then you'll realize that the logic implemented by it is pretty simple to understand. The first thing the method does is open a socket connection to TCP port 110 (the default port for POP3 servers), by utilizing the input parameters passed to this method. As you can see, this operation is quite straightforward, so let's skip over it and analyze the next ones.

    After opening a connection to the POP3 server, things get really interesting. Notice how the method puts the correct sequence, the "USER" and "PASS" commands, in order to perform the corresponding access control process and catch the subsequent responses the server sends back to the client. Finally, the methods ends its operation by transmitting the four-pipe string delimiter to the server. This will be used to split all the server responses into chunks, including the pertinent list of email messages.

    So far, the constructor has been provided with the capacity for connecting to the POP3 server, sending the username/password pair and receiving the responses generated on the server, which turns this method into a useful piece of code. However, as I said before, the class must also be capable of fetching the list of email messages, so they can be visualized within the client program interface. Keeping in mind this requirement, in the next section I'll define another handy class method, named "fetch()," which takes care of retrieving the respective messages.

    More XML Articles
    More By Alejandro Gervasio


       · Over the final installment of this series, the PHP class that talks directly to the...
       · Well written and informative, but it still doesn't work! I copied and pasted the...
       · Thank you for the positive comments on the AJAX-related article I wrote. Regarding...
       · Too bad it still doesn't work for me. Both files are on the same domain, in the same...
       · Hey, thank you for posting your feedback here again. I'm sorry the application is...
       · PHP5 is required.
       · Thank you for posting your feedback here. Yes, you're correct, since the server-side...
       · I have try this application but it did not retrieve any mail message from my inbox....
       · Thank you for commenting on my AJAX article. With reference to your question, you...
       · But when i try using nameko(other application that use php socket),i can retrive the...
       · Can you tell me any mail server that will work with this application?I want to try...
       · Thank you for commenting on my AJAX article again. Unfortunately, I can provide you...
       · I have send you the file..hope you can take a lot and help me on this.By the way I...
       · Thank you for the comments. I alreadly emailed you some source files so you can give...
       · Hai, Any one has idea how to create a web page using strut with ajax.The page...
       · Thank you for commenting on my AJAX article. With reference to your consult, I’ve...
     

    XML ARTICLES

    - Using Regions with XSL Formatting Objects
    - Using XSL Formatting Objects
    - More Schematron Features
    - Schematron Patterns and Validation
    - Using Schematron
    - Datatypes and More in RELAX NG
    - Providing Options in RELAX NG
    - An Introduction to RELAX NG
    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek