Use SOCKET to send emails (PHP) for socket programming)

Source: Internet
Author: User
Tags email account

We know that PHP can send emails in two ways: one is the mail () function, and the other is the socket. For the first method, I will not introduce it much, because it is relatively simple to use, but there may be many things to be configured. For more information, see the PHP documentation. Here, I will mainly talk about the second method.

 

When talking about the second method, I will first talk about the principles of sending emails.

 

Email sending is based on the customer/Server mode. That is to say, the client first needs to send a connection request to the server. after both parties connect, data is transmitted. At the same time, we cannot transmit data at will, but follow certain rules. The rules mentioned here are called protocols.

In the mail system, there are three protocols: SMTP, POP3, and mime. Let's talk about the first two;

SMTP protocol: simple file plug-in protocol used to send emails;

POP3: Protocol for receiving emails.

 

 

 

The above Entity diagram is exactly the principle of sending emails. We usually send an email to another friend instead of sending the email directly to the client of the recipient. It is actually sent to the sender's email server first, then the sender's email server sends the email to the recipient's email server, and the recipient then obtains the required email from the recipient's email server. For example, we have a 163 mailbox with a hl@163.com address; now I want to send an email to the 145642@qq.com mailbox. The actual sending process is to edit the email using a client proxy (such as outlook) on the client, and then click send. Then, the email will be sent to the 163 mail server, 163 the email server will send the email to the QQ mail server at the appropriate time. After receiving the email, the QQ mail server will notify the recipient Based on the recipient's address, the recipient can obtain the email from the QQ mail server. This is the general principle. Of course, the internal working principle is still very complicated.

Let's talk about the protocols involved in the mail sending process. The SMTP protocol is used when we send emails to and from the client proxy to the sending server. When the sender's mail server sends the emails to the receiver's mail server, the SMTP protocol is used. When the recipient's proxy obtains the mail from the recipient's mail server, it uses the POP3 protocol. In a word, the SMTP protocol is used for sending and the POP3 protocol is used for receiving.

 

The socket mail we will discuss here is actually the function of writing code to replace the sender's mail proxy, and sending the mail to the sender's mail server. Here, you will understand it.

 

The following describes the specific implementation:

Because we only care about how to send emails, rather than how the recipient receives the emails, the code we write is how to implement SMTP.

SMTP is divided into the command header and information body. The command header is used to connect the client and server and verify the connection. The entire process consists of multiple commands. After each command is sent to the server, the server provides the corresponding information, usually a three-digit response code and response text. Each Command and response end has a carriage return, so that fputs and fgets can be used to process the response.

 

Some common SMTP commands sent by the client to the mail server are:

Helo hostname greets the server and notifies the Server client of the machine name used. You can enter it here;

Auth login: User, pass email account, password

Mail from: sender tells the mail server the sender's address;

Rcpt to: the recipient's address that the email server receives;

Data starts to send the mail content, and can only end.

Quit transmission ends and the connection is exited;

 

Each time a client sends a command, the server returns the corresponding information in the format of (response code + space + explanation)

Some common codes are as follows:

220 Service ready (socket return)

221 processing in progress

250 successful email request (Helo mail from, rcpt to, and quit command returns this status code)

354 start sending data and end with "." (This message will be returned after the data command is executed successfully );

500 syntax error. The command cannot be identified

550 the command cannot be executed, and the email address is invalid

 

Using socket to send mail refers to sending the above commands to the mail server through the client, so as to realize the SMTP protocol to send mail. In essence, the principle of sending mail by the client mail proxy such as outbllook is the same as the code we want to implement, but they encapsulate these operations and then provide a friendly interface for us to operate.

Below is the code I implemented (with detailed comments): There are many encapsulated and complete mail sending classes on the Internet. I just roughly implemented its functions, and there are still many unimplemented ones, therefore, this is for reference only:

<? Php <br/> class SMTP {<br/> var $ smtp_port; // port of the mail server, which is 25 <br/> var $ host_name; // client host name <br/> var $ relay_host; // email server address <br/> var $ user; // user name <br/> var $ pass; // password <br/> var $ lastmessage; // information returned by the server <br/> private $ socket; // socket handle, returned after fsockopen is called, for details about usage, see the PHP document <br/> // constructor and initialization required information <br/> function _ construct ($ relay_host = "", $ smtp_port = 25, $ auth = false, $ user, $ pass) {<br/> $ this-> smtp_port = $ smtp_port; <br/> $ This-> replay_host = $ relay_host; <br/> $ this-> auth = $ auth; <br/> $ this-> User = $ user; <br/> $ this-> pass = $ pass; <br/> $ this-> host_name = "127.0.0.1 "; <br/>}< br/> // function for executing commands. For details about function usage, see the PHP documentation <br/> function smtp_putcmd ($ cmd) {<br/> fputs ($ this-> socket, $ cmd. "/R/N"); <br/> $ this-> lastmessage = str_replace ("/R/N", "", fgets ($ this-> socket, 512); <br/> If (preg_match ("/^ [23]/", $ this-> lastmessage) <= 0) {// If the status code returned by the email server is not 2 If it starts with 3, an error occurs. For more information about the status code, see <br/> return false; <br/>}< br/> else {<br/> return true; <br/>}< br/> // function used to send emails, the most important <br/> // $ to indicates the recipient address, $ from indicates the sender address, and $ subject indicates the subject of the email, $ body indicates the email subject <br/> function Sendmail ($ to, $ from, $ subject = "", $ body = "") {<br/> $ this-> socket = fsockopen ($ this-> replay_host, $ this-> smtp_port ); // open a socket <br/> stream_set_blocking ($ this-> socket, 1); <br/> $ this-> lastmessage = fgets ($ This-> socket, 512); <br/> If (preg_match ("/^ 220/", $ this-> lastmessage) <= 0) {<br/> return false; <br/>}< br/> else {<br/> // here, the client starts to send the command discussed above to the email server <br/> If (! $ This-> smtp_putcmd ("HELO ". $ this-> host_name) {<br/> die ('error: sending HELO command/N '); <br/>}< br/> if ($ this-> auth) {<br/> If (! $ This-> smtp_putcmd ("auth login ". base64_encode ($ this-> User) {<br/> die ('error: sending HELO command/N'); <br/>}< br/> If (! $ This-> smtp_putcmd (base64_encode ($ this-> pass) {<br/> echo ("falil"); <br/> die ('error: sending HELO command/N'); <br/>}< br/> If (! $ This-> smtp_putcmd ("mail from: <". $ from. ">") {<br/> die ("error: sending mail from command"); <br/>}< br/> If (! $ This-> smtp_putcmd ("rcpt to: <". $. ">") {<br/> die ("error: sending rcpt to Command"); <br/>}< br/> If (! $ This-> smtp_putcmd ("data") {<br/> die ("error: sending data command "); <br/>}< br/> $ head = ":". $. "/R/N"; <br/> $ head. = "from: $ from <". $ from. ">/R/N"; <br/> $ head. = "Subject :". $ subject. "/R/N"; <br/> // $ body = preg_replace ("/(^ | (/R/n ))(/.) /"," // 1. // 3 ", $ body); <br/> $ body. = "/R/n. /R/N "; <br/> fputs ($ this-> socket, $ head. "/R/N ". $ body); <br/> If (! $ This-> smtp_putcmd ("quit") {<br/> die ("error: sending quit command "); <br/>}< br/> // close the socket connection <br/> fclose ($ this-> socket ); <br/>}< br/>?> 

Next, we can send an email:

<? Php <br/> include ('send _ mail. PHP '); <br/> $ Sm = new SMTP ("smtp.163.com", 25, true, "Here is your username", "Here is your password "); <br/> $ Sm-> Sendmail ("Enter the email address you want to send", "Here is your email address", "subject", "subject "); <br/>?> 

As you can see, using socket to send emails is actually very simple. The most important thing is that we need to understand the principles of sending emails.

 

 

 

 

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.