-
- * * Please respect other people's labor success, please keep this copyright information, thank you!
- Author: Little Dew 3.3
- Sailing to fix something: in the code has been annotated, this code is now sent to QQ no problem ~
- */
- Set_time_limit (120);
- Class Smtp_mail
- {
- var $host; Host
- var $port; Port is typically 25
- var $user; SMTP-certified Accounts
- var $pass; Authentication password
- var $debug = false; Do you want to display and server session information?
- var $conn;
- var $result _str; Results
- var $in; Commands sent by the client
- var $from; Source Mailbox
- var $to; Target Mailbox
- var $subject; Theme
- 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 ("Initialize 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 ("Initialize failed, please check your network connection and parameters");
- }
- $this->result_str = "Server Answer:". Socket_read ($this->socket, 1024). "";
- $this->debug_show ($this->result_str);
- }
- function Debug_show ($STR)
- {
- if ($this->debug)
- {
- echo $str. "
\ r \ n ";
- }
- }
- function Send ($from, $to, $subject, $body)
- {
- if ($from = = "" | | $to = = "")
- {
- Exit ("Please enter your email address");
- }
- if ($subject = = "") $sebject = "Untitled";
- if ($body = = "") $body = "no content";
- $this->from = $from;
- $this->to = $to;
- $this->subject = $subject;
- $this->body = $body;
- Sail to modify part of the code
- $All = "from:<". $this->from. " >\r\n ";
- $All. = "to:<". $this->to. " >\r\n ";
- $All. = "Subject:". $this->subject. " \r\n\r\n ";
- $All. = $this->body;
- /*
- If the content of the $all to add processing, you can send a MIME message
- But we need to add a lot of programs.
- */
- The following are the and server sessions
- $this->in = "EHLO helo\r\n";
- $this->docommand ();
- $this->in = "AUTH login\r\n";
- $this->docommand ();
- $this->in = $this->user. " \ r \ n ";
- $this->docommand ();
- $this->in = $this->pass. " \ r \ n ";
- $this->docommand ();
- $this->in = "MAIL from:" $this->from. " \ r \ n ";
- $this->in = "MAIL from:<". $this->from. " >\r\n "; Sail Change
- $this->docommand ();
- $this->in = "RCPT to:" $this->to. " \ r \ n ";
- $this->in = "RCPT to:<". $this->to. " >\r\n "; Sail Change
- $this->docommand ();
- $this->in = "data\r\n";
- $this->docommand ();
- $this->in = $All. " \r\n.\r\n ";
- $this->docommand ();
- $this->in = "quit\r\n";
- $this->docommand ();
- End, close connection
- }
- function DoCommand ()
- {
- Socket_write ($this->socket, $this->in, strlen ($this->in));
- $this->debug_show ("Client command:". $this->in);
- $this->result_str = "Server Answer:". 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, you change it to your own mailbox.
- $SMTP =new smtp_mail ("smtp.qq.com", "+", "yourmail@qq.com", "Your password", true);
- If you need to display the session information, please change the
- $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 Mail");
- ?>
Copy Code
|