Home arrow XML arrow Page 3 - 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 - Fetching email messages: defining the signature of the "fetch()" method
(Page 3 of 4 )

As you've seen before, interacting with the POP3 server is just a matter of injecting the appropriate commands that the server understands right into the socket. Following this approach, the "fetch()" class method uses simple POP3 commands to obtain the full list of existing messages. Take a look at its definition:

public function fetch(){
    fputs($this->fp,"STATn");// send STAT command
    $ret=fgets($this->fp,128).'<br />';
    if(substr($ret,0,5)!='-ERR '){
        $messages=intval(substr($ret,4,1));
        for($i=1;$i<=$messages;$i++){
            fputs($this->fp,"RETR $in"); // send RETR command
            $this->output.=stream_get_contents($this-
>fp).'<br /><br />';// fetch email messages
            $this->output.='||||';// send delimiter string
        }
    }
    $this->output=substr($this->output,0,strlen($this->output)-
4);
    return $ret.$this->output;
}

Here, the method defined above first uses a "STAT" command to check out the status of the server, and then sends out the "RETR" command, which not surprisingly instructs the server to retrieve the list of messages stored in the inbox. Provided no errors occurred during the retrieval process, the method utilizes the "stream_get_contents()" PHP built-in function, in order to read the contents of each message, adding to each one the four-pipe delimiter I showed you when I defined the class constructor.

The last task the method performs is simply to return the complete list of messages in a string format, stored in the "$this->output" property, to be processed later on. As you can see, fetching email messages from the POP3 server is much easier than you might have thought. In fact, it's a matter of sending the proper POP3 commands, so they can be correctly interpreted by the server.

At this point, I provided the PHP class with the ability to fetch email messages, which are returned as a regular string. So, what's the next step? Well, I need to define another class method, called "close()," handy for closing the socket connection to the server. That's precisely what I'll do in the next section.

Closing the connection to the POP3 server: defining the "close()" class method

As you may have guessed, the "close()" class method, tasked with closing the socket connection to the POP3 server, is extremely short in its definition. Actually, it's little more than a simple wrapper for the "fclose()" PHP function, and its signature is as follows:

public function close(){
    fputs($this->fp,"QUITn");
    fclose($this->fp);
}

In short terms, what the above method does is send out a "QUIT" command to the POP3 server, which results in the immediate disconnection from the client program. Additionally, the method also closes the $this->fp handle, opened up when the connection to the server was first established. Simple and efficient.

At this stage, all the pertinent methods of the PHP class have been appropriately defined, so it's a good time to see how they fit together into the general class structure. Definitely, this will allow you to understand much easier how the class works. Bearing in mind this concept, here is the complete definition for the "POP3Processor" class:

class POP3Processor{
    // declare data members
    private $output='';
    private $fp;
    // constructor
    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
    }
    // fetch email messages
    public function fetch(){
        fputs($this->fp,"STATn");// send STAT command
        $ret=fgets($this->fp,128).'<br />';
        if(substr($ret,0,5)!='-ERR '){
            $messages=intval(substr($ret,4,1));
            for($i=1;$i<=$messages;$i++){
                fputs($this->fp,"RETR $in"); // send RETR
command
                $this->output.=stream_get_contents($this-
>fp).'<br /><br />';// fetch email messages
                $this->output.='||||';// send delimiter string
            }
        }
        $this->output=substr($this->output,0,strlen($this-
>output)-4);
        return $ret.$this->output;
    }
    // close mail server connection
    public function close(){
        fputs($this->fp,"QUITn");
        fclose($this->fp);
    }
}

And below there's a possible implementation of this class:

try{
    // instantiate POP3 processor object
    $popProc=new POP3Processor
('pop3hostname',username','password');
    // fetch messages from mail server
    echo $popProc->fetch();
    // close mail server connection
    $popProc->close();
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

As the above example shows, a "POP3Processor" object has been instantiated using the proper input parameters, and a connection to the specified POP3 server has been established. Then, assuming that no error occurred during the connection process, the list of email messages is retrieved and displayed on the web page, by using the "fetch()" method. At the end of the example, the connection is closed by calling the corresponding "close()" method.

All right, now I've demonstrated how you can use the "POP3Processor" class for fetching messages from a POP3 server, but...how does this class interact with the user interface I wrote earlier? Well, in order to illustrate how the AJAX application along with the POP 3 processing class work together, I'll put all the client-side code in one file, called "pop_client.htm," and the PHP class in another one, "pop_processor.php," so you can see in detail how the complete POP3 client works.


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