XML
  Home arrow XML arrow Page 3 - 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 - 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.

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek