Home arrow XML arrow Page 2 - Developing the Server-side Layer of an Email Application in AJAX
XML

Developing the Server-side Layer of an Email Application in AJAX


Here you have it. Welcome to the concluding part of the series “Take AJAX to your email inbox.” In three parts, this series shows in a friendly format how to develop a simple web-based POP3 client which uses AJAX as the driving technology for fetching and displaying email messages on a web document.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
April 12, 2006
TABLE OF CONTENTS:
  1. · Developing the Server-side Layer of an Email Application in AJAX
  2. · Working with a POP3 server: defining the barebones of the "POP3Processor" class
  3. · Fetching email messages: defining the signature of the "fetch()" method
  4. · Assembling the POP3 client: putting client and server-side layers to work together

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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