PHP socket sends mail to verify the true validity of the mailbox rather than the format

Source: Internet
Author: User
  1. * * Please respect other people's labor success, please keep this copyright information, thank you!
  2. Author: Little Dew 3.3
  3. Sailing to fix something: in the code has been annotated, this code is now sent to QQ no problem ~
  4. */
  5. Set_time_limit (120);
  6. Class Smtp_mail
  7. {
  8. var $host; Host
  9. var $port; Port is typically 25
  10. var $user; SMTP-certified Accounts
  11. var $pass; Authentication password
  12. var $debug = false; Do you want to display and server session information?
  13. var $conn;
  14. var $result _str; Results
  15. var $in; Commands sent by the client
  16. var $from; Source Mailbox
  17. var $to; Target Mailbox
  18. var $subject; Theme
  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 ("Initialize 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 ("Initialize failed, please check your network connection and parameters");
  46. }
  47. $this->result_str = "Server Answer:". 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. "

    \ r \ n ";

  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 = "Untitled";
  64. if ($body = = "") $body = "no content";
  65. $this->from = $from;
  66. $this->to = $to;
  67. $this->subject = $subject;
  68. $this->body = $body;
  69. Sail to modify part of the code
  70. $All = "from:<". $this->from. " >\r\n ";
  71. $All. = "to:<". $this->to. " >\r\n ";
  72. $All. = "Subject:". $this->subject. " \r\n\r\n ";
  73. $All. = $this->body;
  74. /*
  75. If the content of the $all to add processing, you can send a MIME message
  76. But we need to add a lot of programs.
  77. */
  78. The following are the and server sessions
  79. $this->in = "EHLO helo\r\n";
  80. $this->docommand ();
  81. $this->in = "AUTH login\r\n";
  82. $this->docommand ();
  83. $this->in = $this->user. " \ r \ n ";
  84. $this->docommand ();
  85. $this->in = $this->pass. " \ r \ n ";
  86. $this->docommand ();
  87. $this->in = "MAIL from:" $this->from. " \ r \ n ";
  88. $this->in = "MAIL from:<". $this->from. " >\r\n "; Sail Change
  89. $this->docommand ();
  90. $this->in = "RCPT to:" $this->to. " \ r \ n ";
  91. $this->in = "RCPT to:<". $this->to. " >\r\n "; Sail Change
  92. $this->docommand ();
  93. $this->in = "data\r\n";
  94. $this->docommand ();
  95. $this->in = $All. " \r\n.\r\n ";
  96. $this->docommand ();
  97. $this->in = "quit\r\n";
  98. $this->docommand ();
  99. End, close 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 Answer:". 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, you change it to your own mailbox.
    4. $SMTP =new smtp_mail ("smtp.qq.com", "+", "yourmail@qq.com", "Your password", true);
    5. If you need to display the session information, please change the
    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 Mail");
    8. ?>
Copy Code
Rather than, send mail, PHP
  • 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.