Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
The mail function provided by php needs to be configured on the server, so it is difficult for beginners to use it to send emails. A large part of developers will use some mail classes found on the Internet to implement this function. Today, I will give you a detailed analysis of the code principles of these mail classes, and strive to take everyone together to write a mail function that comes with the mail Class php to configure the server, therefore, it is difficult for new users to use it to send emails. A large part of developers will use some mail classes found on the Internet to implement this function. Today, I will give you a detailed analysis of the code principles of these mail classes, and strive to write a mail class with everyone.
To complete such a mail class, we need to first understand the socket communication knowledge. Because we do not configure the email server ourselves, we need to rely on other email servers (such as the 163 mailbox server) to send emails to us. It is through socket communication that we can establish communication with the recipient's mailbox server, and then instruct him to send us emails.
Before that, we have already published a special article about socket communication technology in php. here we will not repeat it, if you are not familiar with socket communication, I suggest you take a look at the series of tutorials "getting started with socket communication in PHP" and read this article again.
Assume that we have reached a communication contact with an email server, how can we instruct him to send emails to us? Here we need to introduce the concept of an SMTP protocol.
The SMTP Chinese name is simple mail transmission protocol, which is the standard used in the TCP/IP protocol Group to specify Email exchange during the mail service. Here, we will not discuss SMTP in a long article. if you are interested, you can take a look at the introduction of http://baike.baidu.com/view/5450.htm.
As long as you know, smtp standardizes the mail exchange workflow during the service period and the language of communication between the client and the mail server (we have used these languages, to direct the email server to send emails to us ).
Next, let's take a look at the process specified by smtp when the client needs the mail server to send the mail.
Step 1: When the client tries to connect to the mail server, if the mail server agrees to connect, a 220 error will be returned.
Step 2: When the client receives the 220 message sent by the email server, it will send the HELLO command to the mail server. after receiving the message, the mail server returns a 250 message.
Step 3: After receiving the message 250, the user can request to log on and send the username and password to the email server to verify the client identity.
Step 4: The client sends the sender address to the server.
Step 5: The client sends the inbox address to the server.
Step 6: The client sends the Mail content to the server, starting with DATA and ending with. (the Mail content is divided into message headers and message bodies ).
Step 7: end communication with the email server.
The email server operates on the commands sent by the client and returns the operation results to the client in numbers. For specific feedback values, see smtp return values.
After learning about the principles of SMTP mail sending, we will be very clear about how to implement it with PHP. It is nothing more than 1. connect to the mail server through socket-> 2. send SMTP commands to the mail server-> 3. perform corresponding operations based on the server return values-> 4. encapsulate the above code into a class, for reuse.
Below I provide an SMTP class for sending mails via socket found on the Internet, with detailed comments for your reference. Click to download and unzip the password. add Official QQ to get 2803980493.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.