当前位置:Gxlcms > PHP教程 > phpSocket发送邮件验证邮箱的真实有效性而非格式

phpSocket发送邮件验证邮箱的真实有效性而非格式

时间:2021-07-01 10:21:17 帮助过:18人阅读

  1. /*请尊重别人的劳动成功,请保留此版权信息,谢谢!
  2. 作者:小露珠3.3
  3. 扬帆修正一点东西:在代码中已经用注释注明,本代码现在向qq发信没问题~
  4. */
  5. set_time_limit(120);
  6. class smtp_mail
  7. {
  8. var $host; //主机
  9. var $port; //端口 一般为25
  10. var $user; //SMTP认证的帐号
  11. var $pass; //认证密码
  12. var $debug = false; //是否显示和服务器会话信息?
  13. var $conn;
  14. var $result_str; //结果
  15. var $in; //客户机发送的命令
  16. var $from; //源信箱
  17. var $to; //目标信箱
  18. var $subject; //主题
  19. var $body; //内容
  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); //具体用法请参考手册
  28. if($this->socket)
  29. {
  30. $this->result_str = "创建SOCKET:".socket_strerror(socket_last_error());
  31. $this->debug_show($this->result_str);
  32. }
  33. else
  34. {
  35. exit("初始化失败,请检查您的网络连接和参数");
  36. }
  37. $this->conn = socket_connect($this->socket,$this->host,$this->port);
  38. if($this->conn)
  39. {
  40. $this->result_str = "创建SOCKET连接:".socket_strerror(socket_last_error());
  41. $this->debug_show($this->result_str);
  42. }
  43. else
  44. {
  45. exit("初始化失败,请检查您的网络连接和参数");
  46. }
  47. $this->result_str = "服务器应答:".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("请输入信箱地址");
  62. }
  63. if($subject == "") $sebject = "无标题";
  64. if($body == "") $body = "无内容";
  65. $this->from = $from;
  66. $this->to = $to;
  67. $this->subject = $subject;
  68. $this->body = $body;
  69. //扬帆修改部分代码
  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. 如过把$All的内容再加处理,就可以实现发送MIME邮件了
  76. 不过还需要加很多程序
  77. */
  78. //以下是和服务器会话
  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"; //扬帆修改
  89. $this->docommand();
  90. // $this->in = "RCPT TO:".$this->to."\r\n";
  91. $this->in = "RCPT TO:<".$this->to.">\r\n"; //扬帆修改
  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. //结束,关闭连接
  100. }
  101. function docommand()
  102. {
  103. socket_write ($this->socket, $this->in, strlen ($this->in));
  104. $this->debug_show("客户机命令:".$this->in);
  105. $this->result_str = "服务器应答:".socket_read ($this->socket, 1024)."";
  106. $this->debug_show($this->result_str);
  107. }
  108. }
  109. ?>

php代码

  1. //测试页面
  2. include "smtp_mail.php";
  3. //你用这个类的时候你修改成你自己的信箱就可以了
  4. $smtp=new smtp_mail("smtp.qq.com","25","yourmail@qq.com","Your password",true);
  5. //如果你需要显示会话信息,请将上面的修改成
  6. //$smtp = new smtp_mail("smtp.qq.com","25","你的qq.com的帐号","你的密码",true);
  7. $smtp->send("yourmail@qq.com","yourmail@qq.com","你好","测试邮件");
  8. ?>

而非, 发送邮件, php

人气教程排行