This article mainly introduces PHP based on the SMTP protocol to implement mail sending instance code, has a certain reference value, interested in small partners can refer to
SMTP protocol
When we use PHP's third-party library or tool class to send mail, have you ever thought of a problem:
Why can't we write our own PHP code to realize the message discovery, instead of using someone else's library? How does PHP send the mail exactly how it is implemented?
First of all, we need to understand the basic principle of sending mail, this article based on SMTP protocol to achieve mail delivery
SMTP (Simple Mail Transfer Protocol) is simply a message transfer protocol. In short, it defines a set of rules that we just need to tell the SMTP server about, the sender of the message, the recipient, the content, the subject, and so on.
The SMTP server then resolves the information we send in accordance with this set of rules and finally sends the message.
mail servers such as 163,QQ provide SMTP services, so we can send messages by connecting to their SMTP servers and then write the data.
In fact, we can not write code, directly borrow the Linux Telnet tool to connect the SMTP service, to send mail. Use this to understand the entire process of sending mail.
Telnet for mail sending
We can use the Telnet command in a Linux environment to connect to the 163 SMTP service, 25 ports (typically SMTP is 25 ports) to understand the SMTP transport process.
Telnet smtp.163.com 25
And then we get the following results, which means we're connected.
Trying 220.181.12.16...Connected to Smtp.163.com.escape character are ' ^] '. 163.com anti-spam GT for Coremail System (16 3COM[20141201])
We then execute the following command to tell each other where our identities come from
HELO smtp.163.com
The other side will return to us with an OK
Then execute auth login to tell the other party that we are going to start authentication, and then the other party will respond to some of our messages.
We will then enter our user name, password, email content, sender, recipient and other information, and then end the conversation, the SMTP server will help us send out the mail.
Because the SMTP protocol has strict requirements for the format of the message content, it is not good to execute on the command line, so the entire process is not executed, and the PHP code is used to implement it completely.
The process of connecting SMTP mail using telnet from above can be seen, the process of sending mail is actually very simple, is to connect the SMTP service 25 port, according to the protocol to tell each other what mail we want to send. This is related to the platform, regardless of the programming language.
Whether we use the C language, or Java or PHP, as long as the use of the socket connection to the SMTP server, you can send mail.
SMTP directives
Above we used Telnet to connect the SMTP service, entered some helo, AUTH login, etc., you may have questions about what these are.
In fact, these are the instructions that the SMTP protocol defines, or the rules that the SMTP server uses to know what we want to do.
Common directives are as follows:
instruction |
function |
HELO |
A command that identifies itself to the other mail server |
AUTH LOGIN |
Identity verification is imminent |
MAIL from |
Tell the other person who this email was sent to |
RCPT to |
Send to WHO |
DATA |
Tell the other person this email, and then we send the email details |
QUIT |
After the message content has been entered, execute the command to exit |
PHP for mail delivery
Directly on the code
Class mailer{private $host; Private $port = 25; Private $user; Private $pass; Private $debug = false; Private $sock; Public function __construct ($host, $port, $user, $pass, $debug = False) {$this->host = $host; $this->port = $port; $this->user = Base64_encode ($user); User name password must use base64 encoding $this->pass = Base64_encode ($pass); $this->debug = $debug; Socket connection $this->sock = Fsockopen ($this->host, $this->port); if (! $this->sock) {exit (' error '); }//Read the data returned to us by the SMTP service $response = fgets ($this->sock); $this->debug ($response); If there is a 220 return code in the response, we have successfully connected if (STRSTR ($response, ' size ') = = = False) {exit (' error '); }//Send SMTP instructions, the return code of different instructions may be different public function execcommand ($cmd, $return _code) {fwrite ($this->sock, $cmd); $response = fgets ($this->sock);//output debug information $this->debug (' cmd: '. $cmd. '; Response: '. $response); if (Strstr ($response, $return _code) = = = False) {return false; } return true; } PublIC function SendMail ($from, $to, $subject, $body) {//detail is the content of the message, it must be in strict accordance with the following format, which is the agreement of the $detail = ' from: '. $from. " \ r \ n "; $detail. = ' to: '. $to. " \ r \ n "; $detail. = ' Subject: '. $subject. ' \ r \ n "; $detail. = ' content-type:text/html; '. ' \ r \ n "; $detail. = ' charset=gb2312 '. " \r\n\r\n "; $detail. = $body; $this->execcommand ("HELO". $this->host. " \ r \ n ", 250); $this->execcommand ("AUTH login\r\n", 334); $this->execcommand ($this->user. " \ r \ n ", 334); $this->execcommand ($this->pass. " \ r \ n ", 235); $this->execcommand ("MAIL from:<". $from. " >\r\n ", 250); $this->execcommand ("RCPT to:<". $to. " >\r\n ", 250); $this->execcommand ("data\r\n", 354); $this->execcommand ($detail. " \r\n.\r\n ", 250); $this->execcommand ("quit\r\n", 221); Public Function Debug ($message) {if ($this->debug) {echo ' <p>debug: '. $message. Php_eol. ' </p> '; }} Public Function __destruct () {fclose ($this->sock); }}
Invoke Example
$port =; $user = ' username '; Please replace your own SMTP user name $pass = ' Pass '; Please replace your own smtp password $host = ' smtp.163.com '; $from = ' xxxxx@163.com '; $to = ' xxxx@qq.com '; $body = ' Hello world '; $subjet = ' I am the title '; $mailer = new Mailer ($host, $port, $user, $pass, true); $mailer-& Gt;sendmail ($from, $to, $subjet, $body);
There is output debugging information when executing the instruction, output the instruction we execute each time and the response data that the SMTP service returns to us.
So we can see the following results
debug:220 163.com Anti-Spam GT for Coremail System (163com[20141201]) Debug:cmd:HELO smtp.163.com; response:250 okdebug: Cmd:auth LOGIN; response:334 dXNlcm5hbWU6Debug:cmd:aXR6aG91anVuYmxvZ0AxNjMuY29t; response:334 ugfzc3dvcmq6debug:cmd : qzbjsgrrne32xingfyue5oag==; response:235 authentication SuccessfulDebug:cmd:MAIL from:; response:250 MAIL Okdebug: CMD:RCPT to:<380472723@qq.com>; response:250 Mail OKDebug:cmd:DATA; response:354 End DATA with. debug:cmd:from:itzhoujunblog@163.com to:380472723@qq.com Subject: I am the title content-type:text/html; charset=gb2312 Hello World. ; response:250 Mail OK queued as Smtp11,d8cowacxhe5apdnyco0haq--. 19144s2 1490238785debug:cmd:quit; response:221 Bye
Summarize
Message Delivery steps
Connect to the SMTP service using the socket
Use SMTP commands for conversations, enter identity information, mail messages, and more
End Conversation
The above is the whole content of this article, I hope that everyone's study has helped.