Send mail using the socket

Source: Internet
Author: User

This article is about using the socket to send mail, now share to everyone, interested friends can look at

Previously wrote a message using PHP by using the Nette/mail component to send mail.

The following is compiled from the core technologies and best practices of PHP.
PHP has an own mail () function, but to send mail using the SMTP protocol, you need to install an SMTP server. If you do not want to install, you can use the socket to send mail. The SMTP protocol is based on the TCP protocol, so in principle, the SMTP protocol is used to interact with the SMTP server using the socket.

The SMTP connection and the sending process are as follows:
1) Establish a TCP connection.
2) The client sends the HELO command to identify the sender's own identity, and the client sends the Mail command. The server responds with OK in order to indicate that it is ready to receive.
3) Log in to the SMTP server using the AUTH command, enter the user name and password ( Note that both the user name and password need to be Base64 encrypted ).
4) The client sends the RCPT command, which identifies the intended recipient of the e-mail message, and can have multiple RCPT lines. The server responds with OK, indicating that it is willing to send mail to the recipient.
5) After the negotiation is finished, use the data command to send.
6) with "." The number indicates the end, the input is sent out together, the end of this send, with the Quit command exit.
For example, use Telnet to create an SMTP session where s represents the server, C represents the client, and 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: Enter the user name encrypted with base64 s:334 ugfzc3dvcmq6c: Enter the password with Base64 encrypted s:235 authentication from: <liexusong@qq.com>s:250 Sender <liexusong@qq.com> okc:rcpt to:<liexusong@163.com>s:250 recipient & Lt;liexusong@163.com> okc:datas:354 Enter mail,end with "." On a line by Itselfc:this are example from SMTP Protocolc :. S:250 message sentc:quits:221 Goodbye

Originally want to use QQ mailbox email, but QQ mailbox open SMTP page always error, use 163 mailbox can send mail normally, mailbox need to open POP3/SMTP service.
Code:
smtp_mail.php

<?phpclass smtp_mail{private $host;    Private $port = 25;    Private $user;    Private $pwd;   Private $debug =false;          Whether to turn on debug mode by default does not turn on private $sock; Save a handle to the SMTP server connection private $mail _format=0; Message Format 0 for plain Text 1 for HTML mail 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; Connecting an SMTP server/** * Fsockopen () Initializes a socket connection to the specified primary * Last parameter is timeout, connection time/$this->sock        =fsockopen ($this->host, $this->port, $errno, $ERRSTR, 10);        if (! $this->sock) {//Connection failed exit ("error number: $errno, error message: $ERRSTR \ n");        }//Get server information $response =fgets ($this->sock); If 220, indicates that the server has successfully connected to the IF (Strstr ($response, "") ===false) {//First occurrence address |false exit ("Server error: $response \ n ");       }}//Send the command to the server and then get the server feedback to determine if the command is executed successfully 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; }//Send mail Public function send_mail ($from, $to, $subject, $content) {//To determine if the email address of the transceiver is legitimate if (! $this->is_ema            Il ($from) or! $this->is_email ($to)) {$this->show_debug (' Please enter valid from/to email. ');        return false; }//Determine if the subject content is empty if (empty ($subject) or empty ($content)) {$this->show_debug (' Please enter SUBJEC            T/content. ');        return false; }//To consolidate message information, to send the message body with \r\n.\r\n as the end $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;        Here there should be a decision whether the command executed successfully $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; }//Determine if the private function Is_email ($emial) {if (Filter_var ($emial, Filter_validate_email)) {RET        Urn true;        }else{return false;  }}//display debug information 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 = "Your account name @163.com"; $pwd = "Authorization Code"; $from = " Your account name @163.com "; $to =" target Mailbox "; $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 implementation method to send 16 socket

How PHP implements the socket

Example of PHP socket programming

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.