Home  >  Article  >  Backend Development  >  php Socket sends an email to verify the actual validity of the email address rather than the format

php Socket sends an email to verify the actual validity of the email address rather than the format

WBOY
WBOYOriginal
2016-07-25 08:45:061055browse
  1. /*Please respect the success of other people’s work and keep this copyright information, thank you!
  2. Author: Xiaoluzhu 3.3
  3. Yangfan corrected something: It has been noted in the code with comments. This code now has no problem sending letters to qq~
  4. */
  5. set_time_limit(120);
  6. class smtp_mail
  7. {
  8. var $host ; //Host
  9. var $port; //The port is generally 25
  10. var $user; //SMTP authentication account
  11. var $pass; //Authentication password
  12. var $debug = false; //Whether to display session information with the server ?
  13. var $conn;
  14. var $result_str; //Result
  15. var $in; //Command sent by the client
  16. var $from; //Source mailbox
  17. var $to; //Destination mailbox
  18. var $subject; // Topic
  19. var $body; //Content
  20. function smtp_mail($host,$port,$user,$pass,$debug=false)
  21. {
  22. $this->host = $host;
  23. $this->port = $port;
  24. $this->user = base64_encode($user);
  25. $this->pass = base64_encode($pass);
  26. $this->debug = $debug;
  27. $this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); //Please refer to the manual for specific usage
  28. if($this->socket)
  29. {
  30. $this->result_str = "Create SOCKET:".socket_strerror(socket_last_error());
  31. $this->debug_show($this->result_str);
  32. }
  33. else
  34. {
  35. exit("Initialization failed, please check your network connection and parameters");
  36. }
  37. $this->conn = socket_connect($this->socket,$this->host,$this->port);
  38. if($this->conn)
  39. {
  40. $this->result_str = "Create SOCKET connection:" .socket_strerror(socket_last_error());
  41. $this->debug_show($this->result_str);
  42. }
  43. else
  44. {
  45. exit("Initialization failed, please check your network connection and parameters");
  46. }
  47. $this->result_str = "Server response: ".socket_read ($this->socket, 1024)."";
  48. $this->debug_show( $this->result_str);
  49. }
  50. function debug_show($str)
  51. {
  52. if($this->debug)
  53. {
  54. echo $str."

    rn";

  55. }
  56. }
  57. function send($from,$to,$subject,$body)
  58. {
  59. if($from == "" || $to == "")
  60. {
  61. exit("Please enter your email address");
  62. }
  63. if($subject == "") $sebject = "No title";
  64. if($body == "") $body = "No content";
  65. $this->from = $from;
  66. $this ->to = $to;
  67. $this->subject = $subject;
  68. $this->body = $body;
  69. //Yangfan modified part of the code
  70. $All = "From:<".$ this->from.">rn";
  71. $All .= "To:<".$this->to.">rn";
  72. $All .= "Subject:".$this- >subject."rnrn";
  73. $All .= $this->body;
  74. /*
  75. If you process the content of $All, you can send MIME emails
  76. But you still need to add a lot of programs
  77. */
  78. //The following is a session with the server
  79. $this->in = "EHLO HELOrn";
  80. $this->docommand();
  81. $this->in = "AUTH LOGINrn";
  82. $ this->docommand();
  83. $this->in = $this->user."rn";
  84. $this->docommand();
  85. $this->in = $this- >pass."rn";
  86. $this->docommand();
  87. // $this->in = "MAIL FROM:".$this->from."rn";
  88. $this- >in = "MAIL FROM:<".$this->from.">rn"; // Yangfan modified
  89. $this->docommand();
  90. // $this->in = "RCPT TO:".$this->to."rn";
  91. $this->in = "RCPT TO:<".$this->to.">rn"; //Sail modification
  92. $this->docommand();
  93. $this->in = "DATArn";
  94. $this->docommand();
  95. $this->in = $All."rn.rn" ;
  96. $this->docommand();
  97. $this->in = "QUITrn";
  98. $this->docommand();
  99. //End, close the connection
  100. }
  101. function docommand()
  102. {
  103. socket_write ($this->socket, $this->in, strlen ($this->in));
  104. $this->debug_show("Client command: ".$this-> in);
  105. $this->result_str = "Server response: ".socket_read ($this->socket, 1024)."";
  106. $this-> ;debug_show($this->result_str);
  107. }
  108. }
  109. ?>
Copy code

php code

  1. //Test page
  2. include "smtp_mail.php";
  3. //When you use this class, just modify it to your own mailbox
  4. $smtp =new smtp_mail("smtp.qq.com","25","yourmail@qq.com","Your password",true);
  5. //If you need to display session information, please change the above to
  6. / /$smtp = new smtp_mail("smtp.qq.com","25","your qq.com account","your password",true);
  7. $smtp->send("yourmail@qq .com","yourmail@qq.com","Hello","Test Email");
  8. ?>
Copy code

Instead of sending email, php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn