Guest User

Untitled

a guest
Sep 21st, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * 验证邮箱是否存在的类
  5. *
  6. * @author widuu <admin@widuu.com>
  7. */
  8.  
  9. namespace framework\system;
  10.  
  11. class verifyEmail{
  12.  
  13. /**
  14. * 连接超时时间
  15. */
  16.  
  17. CONST CONN_TIMEOUT = 10;
  18.  
  19. /**
  20. * 读取超时时间
  21. */
  22.  
  23. CONST READ_TIMEOUT = 5;
  24.  
  25. /**
  26. * 打开端口
  27. */
  28.  
  29. CONST SMTP_PORT = 25;
  30.  
  31. private function __construct(){}
  32.  
  33. /**
  34. * 验证邮箱方法
  35. *
  36. * @param $email string 要验证的邮箱
  37. * @return array status = [0=>'不存在',1=>'存在'] errMsg=>错误信息|查询信息
  38. */
  39.  
  40. static public function verify($email = '')
  41. {
  42. if( empty($email) ){
  43. return ['status'=>0,'errMsg'=>'邮箱不能为空'];
  44. }
  45.  
  46. // 验证邮箱地址是否错误
  47. $email_info = self::getEmailInfo($email);
  48. if( is_null($email_info) ){
  49. return ['status'=>0,'errMsg'=>'邮箱地址错误'];
  50. }
  51.  
  52. // 验证DNS是否存在
  53. $mx_info = self::verifyMax($email_info['host']);
  54. if( !$mx_info ){
  55. return ['status'=>0,'errMsg'=>'DNS 解析不存在'];
  56. }
  57.  
  58. // 验证用户是否存在
  59. $result = self::verifyUser($email_info, $mx_info['data']);
  60. if( $result['status'] ){
  61. return ['status'=>1, 'errMsg'=>$result['msg'] ];
  62. }else{
  63. return ['status'=>0, 'errMsg'=>$result['msg'] ];
  64. }
  65.  
  66. }
  67.  
  68. /**
  69. * 验证邮箱是否存在
  70. *
  71. * @author widuu <admin@widuu.com>
  72. */
  73.  
  74. private static function verifyUser( $email_info = [] ,$host_array = [] )
  75. {
  76. $sock = null;
  77. $hostname = '';
  78. foreach($host_array as $weight => $host)
  79. {
  80. if($sock = @fsockopen($host,self::SMTP_PORT,$errno,$errstr,self::CONN_TIMEOUT))
  81. {
  82. $hostname = $host;
  83. stream_set_timeout($sock,self::READ_TIMEOUT);
  84. break;
  85. }
  86. }
  87. if( !$sock ){
  88. return false;
  89. }
  90. $resp[0] = "Connection succeeded to {$hostname} SMTP.";
  91. $resp[1] = fgets($sock,1024);
  92. $resp[2] = "HELO baidu.com";
  93. fputs($sock, "HELO baidu.com\r\n");
  94. $resp[3] = fgets($sock,1024);
  95. $resp[4] = "MAIL FROM:<notice@baidu.com>";
  96. fputs($sock, "MAIL FROM:<notice@baidu.com>\r\n");
  97. $resp[5] = fgets($sock,1024);
  98. $resp[6] = "RCPT TO:<".$email_info['user']."@".$email_info['host'].">";
  99. fputs($sock, "RCPT TO:<".$email_info['user']."@".$email_info['host'].">\r\n");
  100. $resp[7] = fgets($sock,1024);
  101. $vaild = (preg_match('/250|451|452\s/',$resp[7]) == 1);
  102. fclose($sock);
  103. return ['status'=>$vaild, 'msg'=>$resp];
  104. }
  105.  
  106. /**
  107. * 验证邮箱的MAX记录
  108. *
  109. * @author widuu <admin@widuu.com>
  110. */
  111.  
  112. private static function verifyMax($hostname = '')
  113. {
  114. $mxhosts = array();
  115. $weight = array();
  116. $result = getmxrr($hostname, $mxhosts, $weight);
  117. if( !$result ){
  118. return false;
  119. }
  120. $mx_array = array_combine($weight, $mxhosts);
  121. arsort($mx_array,SORT_NUMERIC);
  122. $return_info = [ 'hostname'=>$mxhosts[0], 'data'=>$mx_array ];
  123. return $return_info;
  124. }
  125.  
  126. /**
  127. * 正则表达式验证是否是邮箱
  128. *
  129. * @author widuu <admin@widuu.com>
  130. */
  131.  
  132. private static function getEmailInfo($email)
  133. {
  134. $emailRegex = <<<__REGEX__
  135. /(?P<user>
  136. \w+([-+.]\w+)*
  137. )@(?P<host>
  138. \w+([-.]\w)*\.\w+([-.]\w+)*
  139. )/x
  140. __REGEX__;
  141. return (preg_match($emailRegex,$email,$matches))? $matches : null ;
  142. }
  143. }
  144.  
  145. var_dump(verifyEmail::verify('widuu@baidu.com'));
Add Comment
Please, Sign In to add comment