- /*Please respect the success of other people’s work and keep this copyright information, thank you!
- Author: Xiaoluzhu 3.3
- Yangfan corrected something: It has been noted in the code with comments. This code now has no problem sending letters to qq~
- */
- set_time_limit(120);
- class smtp_mail
- {
- var $host ; //Host
- var $port; //The port is generally 25
- var $user; //SMTP authentication account
- var $pass; //Authentication password
- var $debug = false; //Whether to display session information with the server ?
- var $conn;
- var $result_str; //Result
- var $in; //Command sent by the client
- var $from; //Source mailbox
- var $to; //Destination mailbox
- var $subject; // Topic
- var $body; //Content
- function smtp_mail($host,$port,$user,$pass,$debug=false)
- {
- $this->host = $host;
- $this->port = $port;
- $this->user = base64_encode($user);
- $this->pass = base64_encode($pass);
- $this->debug = $debug;
- $this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); //Please refer to the manual for specific usage
- if($this->socket)
- {
- $this->result_str = "Create SOCKET:".socket_strerror(socket_last_error());
- $this->debug_show($this->result_str);
- }
- else
- {
- exit("Initialization failed, please check your network connection and parameters");
- }
- $this->conn = socket_connect($this->socket,$this->host,$this->port);
- if($this->conn)
- {
- $this->result_str = "Create SOCKET connection:" .socket_strerror(socket_last_error());
- $this->debug_show($this->result_str);
- }
- else
- {
- exit("Initialization failed, please check your network connection and parameters");
- }
- $this->result_str = "Server response: ".socket_read ($this->socket, 1024)."";
- $this->debug_show( $this->result_str);
-
- }
- function debug_show($str)
- {
- if($this->debug)
- {
- echo $str."
rn";
- }
- }
- function send($from,$to,$subject,$body)
- {
- if($from == "" || $to == "")
- {
- exit("Please enter your email address");
- }
- if($subject == "") $sebject = "No title";
- if($body == "") $body = "No content";
- $this->from = $from;
- $this ->to = $to;
- $this->subject = $subject;
- $this->body = $body;
-
- //Yangfan modified part of the code
- $All = "From:<".$ this->from.">rn";
- $All .= "To:<".$this->to.">rn";
- $All .= "Subject:".$this- >subject."rnrn";
- $All .= $this->body;
- /*
- If you process the content of $All, you can send MIME emails
- But you still need to add a lot of programs
- */
-
- //The following is a session with the server
- $this->in = "EHLO HELOrn";
- $this->docommand();
-
- $this->in = "AUTH LOGINrn";
- $ this->docommand();
-
- $this->in = $this->user."rn";
- $this->docommand();
-
- $this->in = $this- >pass."rn";
- $this->docommand();
-
- // $this->in = "MAIL FROM:".$this->from."rn";
- $this- >in = "MAIL FROM:<".$this->from.">rn"; // Yangfan modified
- $this->docommand();
-
- // $this->in = "RCPT TO:".$this->to."rn";
- $this->in = "RCPT TO:<".$this->to.">rn"; //Sail modification
- $this->docommand();
-
- $this->in = "DATArn";
- $this->docommand();
-
- $this->in = $All."rn.rn" ;
- $this->docommand();
-
- $this->in = "QUITrn";
- $this->docommand();
-
- //End, close the connection
-
- }
- function docommand()
- {
- socket_write ($this->socket, $this->in, strlen ($this->in));
- $this->debug_show("Client command: ".$this-> in);
- $this->result_str = "Server response: ".socket_read ($this->socket, 1024)."";
- $this-> ;debug_show($this->result_str);
- }
- }
- ?>
Copy code
php code
- //Test page
- include "smtp_mail.php";
- //When you use this class, just modify it to your own mailbox
- $smtp =new smtp_mail("smtp.qq.com","25","yourmail@qq.com","Your password",true);
- //If you need to display session information, please change the above to
- / /$smtp = new smtp_mail("smtp.qq.com","25","your qq.com account","your password",true);
- $smtp->send("yourmail@qq .com","yourmail@qq.com","Hello","Test Email");
- ?>
Copy code
|