Development of Web email client based on AJAX technology-communication with email server

Source: Internet
Author: User
Http://www.51cto.com Author: Zhu Xianzhong compiled source: 51CTO.com


[Guide] In this article, we will analyze how to create a PHP class to implement communication between our Web email client and the mail server.

I. Introduction

In the second part of this series, I show you all the JavaScript Functions that constitute the entire POP3 client. To retrieve and display email messages on the web page, and create a function to add appropriate behaviors to each user interface control, I define a function to send and process HTTP requests.

At the end of Article 2, we created a client application layer with complete functions. This layer can request the PHP file-it directly communicates with a specified POP3 server and extracts the message to be displayed to display on the Web page. Although the user interface I developed in the previous article exposes some basic controls for visualization and message navigation, you may want to add your own new features to create a better program. To this end, you can improve it based on the examples provided by me, or create a brand new interface and provide more complex navigation controls, or a truly professional look. As you can imagine, there are still a lot of extensions in this program.

In this article (the last article), I will connect the client application layer created in the previous article to the mail server and execute the POP3 command, in this way, the email message is retrieved and displayed on the corresponding webpage. To achieve this goal, I will develop a PHP class that is easy to understand. It provides some useful methods to build a connection to the POP3 server and extract the email messages.

Next, let's start creating this PHP POP3 processing class.

Ii. Use POP3 server-define the basic framework of the POP3Processor class

Assuming that you have correctly understood the JavaScript function defined in the previous article, we will focus on developing a PHP class. This class is responsible for connecting to the mail server, pushing the original POP3 command and retrieving the email message list for future display.

To implement the above tasks, the PHP class I developed will expose three core methods. The first is the constructor, which is responsible for using the POP3 authentication command to establish a connection to a given mail server. The second method, fetch (), is responsible for sending required commands to retrieve formatted email message lists from the inbox. Finally, the close () method will close the socket connection to the server.

Corresponding to the task described above, the basic framework of the POP3Processor class can be defined as follows:

Class POP3Processor {

// Connect to the POP3 server

Public function _ construct (){

// Code for connecting to the POP3 server

}

// Retrieve the email message

Public function fetch (){

// The code for retrieving the email message here

}

// Close the email server connection

Public function close (){

// The code for disabling POP3 mail server connection is as follows:

}

}

As you can see, the structure of the above class follows the general rules, so it is easier to read and understand. Of course, this is also the only framework of this class. Next, we must define each related class method. First, we define the first method of this class.

3. Connect to the POP3 server-define the POP3Processor class constructor Method

To handle all operations related to the connection to the POP3 server, this class uses its constructor method. This method accepts the input parameters (that is, its name or IP address, and name/password combination) that are normally connected to a specific server ). The specific implementation of this method is as follows:

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 the USER command

$ This-> output. = fgets ($ this-> fp, 128). '<br/> ';

Fputs ($ this-> fp, "PASS $ passwordn"); // send the PASS Command

$ This-> output. = fgets ($ this-> fp, 128). '<br/> ';

$ This-> output. = '|'; // sending limit string

}

If you have studied the above method, you will find that its implementation logic is quite simple. The first thing to achieve this method is to open a socket connection to TCP port 110 (default POP3 server port number) by using the input parameters of this method. As you can see, this operation is quite direct, so we will not analyze it here.

After opening a connection to the POP3 server, things become more and more interesting. Please note that in order to execute the corresponding access control process and obtain the response sent back to the client by the server, this method successively sends "USER" and "PASS" commands. Finally, this method ends operations by transmitting the four pipeline string delimiters to the server. They are used to split all server responses (including the list of related email messages) into blocks.

At this point, the constructor has connected to the POP3 server, sent a user name/password pair, and received the response generated on the server. However, as I mentioned earlier, this class must also retrieve the list of email messages to visualize these messages in the client interface. In the next section, I will define another class method fetch () for Retrieving messages ().

Iv. retrieve email message-define the fetch () method

As you have seen before, to interact with the POP3 server, you only need to inject appropriate commands that the server can understand into the socket. Following this idea, the fetch () method uses a simple POP3 command to obtain a complete list of existing messages. See the following definition:

Public function fetch (){

Fputs ($ this-> fp, "STATn"); // send the 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 the RETR command

$ This-> output. = stream_get_contents ($ this-

> Fp). '<br/>'; // retrieve the email message.

$ This-> output. = '|'; // sending limit string

}

}

$ This-> output = substr ($ this-> output, 0, strlen ($ this-> output )-

4 );

Return $ ret. $ this-> output;

}

The above method first uses the "STAT" command to check the server status, and then sends the "RETR" command, which instructs the server to retrieve the list of messages stored in the inbox. If no error occurs during the retrieval process, this method uses the "stream_get_contents ()" PHP built-in function to read the content of each message, add the four pipeline delimiters displayed to you when I define the class constructor after each entry.

Finally, this method returns the complete message list in string format and stores them in the "$ this-> output" attribute for later processing. As you can see, retrieving email messages from the POP3 server is much easier than you think. In fact, this is only for sending appropriate POP3 commands because they can be correctly interpreted by the server.

So far, the PHP class I provided has been able to retrieve email messages (returned in a regular string ). Next, I need to define another class method close () to close the connection to the server.
5. close the connection to the POP3 server-define the close () method

You may have guessed that the close () method is responsible for closing the socket connection to the POP3 server, and the definition of this method is very short. In fact, it is just a simple package of the PHP fclose () function. The specific implementation code is as follows:

public function close(){

fputs($this->fp,"QUITn");

fclose($this->fp);

}

The above method sends a "QUIT" command to the POP3 server, which causes immediate interruption of the server and client programs. In addition, this method also closes the handle $ this-> fp-which was created when the first connection to the server was established.

So far, we have defined all methods of the PHP class. Next, let's take a look at how they are applied together to a class structure. Obviously, this will help you better understand how this class works. The complete definition of this POP3Processor class is as follows:

Class POP3Processor {

// Declare a data member

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 the USER command

$ This-> output. = fgets ($ this-> fp, 128). '<br/> ';

Fputs ($ this-> fp, "PASS $ passwordn"); // send the PASS Command

$ This-> output. = fgets ($ this-> fp, 128). '<br/> ';

$ This-> output. = '|'; // sending limit string

}

// Retrieve the email message

Public function fetch (){

Fputs ($ this-> fp, "STATn"); // send the 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 the RETR command

$ This-> output. = stream_get_contents ($ this-

> Fp). '<br/>'; // retrieve the email message.

$ This-> output. = '|'; // send a bounded string

}

}

$ This-> output = substr ($ this-> output, 0, strlen ($ this-

> Output)-4 );

Return $ ret. $ this-> output;

}

// Close the email server connection

Public function close (){

Fputs ($ this-> fp, "QUITn ");

Fclose ($ this-> fp );

}

}

Below is a possible implementation of this class:

Try {

// Instantiate a POP3 processor object

$ PopProc = new POP3Processor ('pop3hostname', username', 'Password ');

// Retrieve the message from the email server

Echo $ popProc-> fetch ();

// Close the email server connection

$ PopProc-> close ();

}

Catch (Exception $ e ){

Echo $ e-> getMessage ();

Exit ();

}

As shown in the preceding example, A POP3Processor object is instantiated using an appropriate input parameter and a connection is established to the specified POP3 server. Then, if no error occurs during connection processing, then the email message will be retrieved and displayed on the Web page. This is achieved through the fetch () method. At the end of the example, close the connection by calling the corresponding close () method.

Now I have shown you how to use the POP3Processor class to retrieve messages from a POP3 server. But how does this class interact with the user interface class I created earlier? To demonstrate how the AJAX application works with the POP flood file and put the PHP class in another file pop_processor.php, you can easily see how the complete POP3 client works.

6. Assemble POP3-combine the client and server layer

As mentioned above, the file pop_client.htm forms a web-based POP3 client. This file interacts with the PHP3Processor class to connect to the POP3 server and retrieve the mail message list from it. Because the code of this file is long, please refer to the attached source code file (detailed Chinese notes have been added ).

VII. Summary

Now, we have all ended. In this series of articles, I show you another application of AJAX technology-creating a Web-based POP3 client application, it can retrieve email messages from an email server and display them on a Web page. Of course, this program is only the beginning of the development of more complex projects; but with this program, you should be able to have a clearer understanding of how to use AJAX to build a Web program similar to a desktop application.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.