[PHP实例] php Socket发送邮件验证邮箱的真实有效性而非格式

来源:互联网 发布:杭州淘宝培训机构 编辑:程序博客网 时间:2024/04/28 04:43
  1. <?php
  2. /*请尊重别人的劳动成功,请保留此版权信息,谢谢!
  3. 作者:小露珠3.3
  4. 扬帆修正一点东西:在代码中已经用注释注明,本代码现在向qq发信没问题~
  5. */
  6. set_time_limit(120);
  7. class smtp_mail
  8. {
  9. var $host; //主机
  10. var $port; //端口 一般为25
  11. var $user; //SMTP认证的帐号
  12. var $pass; //认证密码
  13. var $debug = false; //是否显示和服务器会话信息?
  14. var $conn;
  15. var $result_str; //结果
  16. var $in; //客户机发送的命令
  17. var $from; //源信箱
  18. var $to; //目标信箱
  19. var $subject; //主题
  20. var $body; //内容
  21. function smtp_mail($host,$port,$user,$pass,$debug=false)
  22. {http://www.nvzi91.cn/jiankang/29973.html
  23. $this->host = $host;
  24. $this->port = $port;
  25. $this->user = base64_encode($user);
  26. $this->pass = base64_encode($pass);
  27. $this->debug = $debug;
  28. $this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); //具体用法请参考手册
  29. if($this->socket)
  30. {http://www.nvzi91.cn/chunvmoxiufu/29974.html
  31. $this->result_str = "创建SOCKET:".socket_strerror(socket_last_error());
  32. $this->debug_show($this->result_str);
  33. }
  34. else
  35. {
  36. exit("初始化失败,请检查您的网络连接和参数");
  37. }
  38. $this->conn = socket_connect($this->socket,$this->host,$this->port);
  39. if($this->conn)
  40. {
  41. $this->result_str = "创建SOCKET连接:".socket_strerror(socket_last_error());
  42. $this->debug_show($this->result_str);
  43. }
  44. else
  45. {http://www.nvzi91.cn/zhuanti/leep/29975.html
  46. exit("初始化失败,请检查您的网络连接和参数");
  47. }
  48. $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
  49. $this->debug_show($this->result_str);
  50. }
  51. function debug_show($str)
  52. {
  53. if($this->debug)
  54. {
  55. echo $str."<p>\r\n";
  56. }
  57. }http://www.nvzi91.cn/gongjingxirou/29976.html
  58. function send($from,$to,$subject,$body)
  59. {
  60. if($from == "" || $to == "")
  61. {
  62. exit("请输入信箱地址");
  63. }http://www.nvzi91.cn/fujianyan/29977.html
  64. if($subject == "") $sebject = "无标题";
  65. if($body == "") $body = "无内容";
  66. $this->from = $from;
  67. $this->to = $to;
  68. $this->subject = $subject;
  69. $this->body = $body;
  70. //扬帆修改部分代码
  71. $All = "From:<".$this->from.">\r\n";
  72. $All .= "To:<".$this->to.">\r\n";
  73. $All .= "Subject:".$this->subject."\r\n\r\n";
  74. $All .= $this->body;
  75. /*
  76. 如过把$All的内容再加处理,就可以实现发送MIME邮件了
  77. 不过还需要加很多程序
  78. */
  79. //以下是和服务器会话
  80. $this->in = "EHLO HELO\r\n";
  81. $this->docommand();
  82. $this->in = "AUTH LOGIN\r\n";
  83. $this->docommand();
  84. $this->in = $this->user."\r\n";
  85. $this->docommand();
  86. $this->in = $this->pass."\r\n";
  87. $this->docommand();
  88. www.nvzi91.cn
  89. // $this->in = "MAIL FROM:".$this->from."\r\n";
  90. $this->in = "MAIL FROM:<".$this->from.">\r\n"; //扬帆修改
  91. $this->docommand();
  92. // $this->in = "RCPT TO:".$this->to."\r\n";
  93. $this->in = "RCPT TO:<".$this->to.">\r\n"; //扬帆修改
  94. $this->docommand();
  95. $this->in = "DATA\r\n";
  96. $this->docommand();
  97. $this->in = $All."\r\n.\r\n";
  98. $this->docommand();
  99. $this->in = "QUIT\r\n";
  100. $this->docommand();
  101. //结束,关闭连接
  102. }
  103. function docommand()
  104. {
  105. socket_write ($this->socket, $this->in, strlen ($this->in));
  106. $this->debug_show("客户机命令:".$this->in);
  107. $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
  108. $this->debug_show($this->result_str);
  109. }
  110. }
  111. ?>
复制代码

php代码

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