Home > Article > Backend Development > Send email using Socket
This article is about using Socket to send emails. Now I share it with you. Friends who are interested can take a look.
I wrote an article before "Using PHP to send emails". The method is to use nette/mail Component sends email.
The following content is compiled from "PHP Core Technology and Best Practices".
PHP has a built-in mail() function, but if you want to use the SMTP protocol to send emails, you need to install an SMTP server. If you don't want to install it, you can use Socket to send emails. The SMTP protocol is built on the TCP protocol, so in principle, Socket is used to interact with the SMTP server according to the specifications of the SMTP protocol.
The SMTP connection and sending process is as follows:
1) Establish a TCP connection.
2) The client sends the HELO command to identify the sender himself, and the client sends the MAIL command. The server responds with OK, indicating that it is ready to receive.
3) Use the AUTH command to log in to the SMTP server and enter the username and password (Note that both the username and password need to be base64 encrypted).
4) The client sends an RCPT command to identify the intended recipient of the email. There can be multiple RCPT lines. The server responds with OK, indicating that it is willing to send the email to the recipient.
5) After the negotiation is completed, use the DATA command to send.
6) End with ".", and send the input content together. End this sending and exit with the QUIT command.
For example, use Telnet to create an SMTP session, where S represents the server and C represents the client. The code is as follows:
C: open smtp.qq.com 25S: 220 esmtp4.qq.com Esmtp QQ Mail ServerC: HELO smtp qq.comS: 250 esmtp4.qq.comC: AUTH loginS: 334 VXNlcm5hbWU6C: 这里输入使用base64加密过的用户名S: 334 UGFzc3dvcmQ6C: 这里输入使用base64加密过的密码 S:235 Authentication successfulC: MAIL FROM:<liexusong@qq.com>S: 250 sender <liexusong@qq.com> OKC: RCPT TO:<liexusong@163.com>S: 250 recipient <liexusong@163.com> OKC: DATAS: 354 Enter mail,end with "." on a line by itselfC: This is example from smtp protocolC:.S: 250 message sentC: QUITS: 221 goodbye
I originally wanted to use qq mailbox to send emails, but the page always errors after qq mailbox opens SMTP. , you can send emails normally using 163 mailbox, and the mailbox needs to enable POP3/SMTP service.
Code:
smtp_mail.php
<?php class smtp_mail{ private $host; private $port=25; private $user; private $pwd; private $debug=false; //是否开启调试模式 默认不开启 private $sock; //保存与SMTP服务器连接的句柄 private $mail_format=0; //邮件格式 0为普通文本 1为HTML邮件 public function smtp_mail($host,$port,$user,$pwd,$mail_format=0,$debug=false){ $this->host=$host; $this->port=$port; $this->user=$user; $this->pwd=$pwd; $this->mail_format=$mail_format; $this->debug=$debug; //连接SMTP服务器 /** * fsockopen() 初始化一个套接字连接到指定主 * 最后一个参数为timeout,连接时限 */ $this->sock=fsockopen($this->host,$this->port,$errno,$errstr,10); if (!$this->sock){//连接失败 exit("Error number: $errno, Error message: $errstr\n"); } //取得服务器信息 $response=fgets($this->sock); //若包含220,表示已经成功连接到服务器 if (strstr($response,"220")===false){//首次出现地址|false exit("Server error: $response\n"); } } //将命令发送到服务器,然后取得服务器反馈信息,判断命令是否执行成功 private function do_command($cmd,$return_code){ fwrite($this->sock,$cmd); $response=fgets($this->sock); if (strstr($response,"$return_code")===false){ $this->show_debug($response); return false; } return true; } //发送邮件 public function send_mail($from,$to,$subject,$content){ //判断收发件邮箱地址是否合法 if (!$this->is_email($from) or !$this->is_email($to)){ $this->show_debug('Please enter valid from/to email.'); return false; } //判断主题内容是否为空 if (empty($subject) or empty($content)){ $this->show_debug('Please enter subject/content.'); return false; } //整合邮件信息,发送邮件主体时要以\r\n.\r\n作为结尾 $detail="From:".$from."\r\n"; $detail.="To:".$to."\r\n"; $detail.="Subject:".$subject."\r\n"; if ($this->mail_format==1){ $detail.="Content-Type: text/html;\r\n"; }else{ $detail.="Content-Type: text/plain;\r\n"; } $detail.="charset=utf-8\r\n\r\n"; $detail.=$content; //此处应该有判断命令是否执行成功 $this->do_command("HELO smtp.qq.com\r\n",250); $this->do_command("AUTH LOGIN\r\n",334); $this->do_command("$this->user\r\n",334); $this->do_command("$this->pwd\r\n",235); $this->do_command("MAIL FROM:<".$from.">\r\n",250); $this->do_command("RCPT TO:<".$to.">\r\n",250); $this->do_command("DATA\r\n",354); $this->do_command($detail."\r\n.\r\n",250); $this->do_command("QUIT\r\n",221); return true; } //判断是否为合法邮箱 private function is_email($emial){ if(filter_var($emial,FILTER_VALIDATE_EMAIL)){ return true; }else{ return false; } } //显示调试信息 private function show_debug($message){ if ($this->debug){ echo "<p>Debug: $message</p><br/>"; } } }
index.php
<?phpinclude_once "smtp_mail.php";$host="smtp.163.com";$port=25;$user="你的账户名@163.com";$pwd="授权码";$from="你的账户名@163.com";$to="目标邮箱";$subject="Test Smtp Mail";$content="This is example email for you.";$mail=new smtp_mail($host,$port,base64_encode($user),base64_encode($pwd),1,true);$mail->send_mail($from,$to,$subject,$content);
Related recommendations:
php method to implement sending hexadecimal socket
How to implement socket in php
Detailed example of socket programming in php
The above is the detailed content of Send email using Socket. For more information, please follow other related articles on the PHP Chinese website!