Development of Web email client and email server communication based on AJAX technology (1)

Source: Internet
Author: User

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 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 normally connected to a specific server, that is, its name or IP address, and the name/password combination ). 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 ).'
';
Fputs ($ this-> fp, "USER $ usern"); // send the USER command
$ This-> output. = fgets ($ this-> fp, 128 ).'
';
Fputs ($ this-> fp, "PASS $ passwordn"); // send the PASS Command
$ This-> output. = fgets ($ this-> fp, 128 ).'
';
$ 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 the default POP3 server port number of TCP port 110-implemented 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 break down all server responses, including the list of related email messages.

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 ).'
';
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 ).'

'; // 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 and return them as a regular string ). Next, I need to define another class method close () to close the connection to the server.


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.