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.
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:
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.