[PHP] php对Email邮箱地址进行邮件验证强验证 →→→→→进入此内容的聊天室

来自 , 2020-03-26, 写在 PHP, 查看 116 次.
URL http://www.code666.cn/view/35937e34
  1. <?php
  2.  
  3. /*
  4.  *  __construct($email)     takes an email address to check
  5.  *  
  6.  * simpleCheck()            Tests to see if an email address is formatted correctly
  7.  *                          and the domain it belongs to exists, such as: gmail.com, yahoo.com
  8.  *  
  9.  * strongCheck()            Tests to see if an email address is valid and that the
  10.  *                          email actually accepts emails by actually connecting to the server.
  11.  *                          Note: strongCheck() can be slow
  12.  */
  13.  
  14. class EmailValidator{
  15.  
  16.     private $email  = "";
  17.     private $mxhost = "";
  18.  
  19.     public function __construct($email){
  20.         $this->email  = $email;
  21.         $this->mxhost = $this->getMXHost();
  22.     }
  23.  
  24.     public function strongCheck(){
  25.         if(filter_var($this->email, FILTER_VALIDATE_EMAIL) && $this->fConnect()){
  26.             return true;
  27.         }
  28.         return false;
  29.     }
  30.  
  31.     public function simpleCheck(){
  32.         if(filter_var($this->email, FILTER_VALIDATE_EMAIL) && $this->getMXHost()){
  33.             return true;
  34.         }
  35.         return false;
  36.     }
  37.  
  38.     private function fConnect(){
  39.         $fp             = @fsockopen($this->mxhost, 25, $errno, $errstr, 5);
  40.         $ms_resp        = "";
  41.         $b_server_found = false;
  42.         if($fp){
  43.             $ms_resp .= $this->send_command($fp, "HELO hi");
  44.             $ms_resp .= $this->send_command($fp, "MAIL FROM:<ryan@ryannaddy.com>");
  45.             $rcpt_text = $this->send_command($fp, "RCPT TO:<{$this->email}>");
  46.             $ms_resp .= $rcpt_text;
  47.             if(substr($rcpt_text, 0, 3) == "250"){
  48.                 $b_server_found = true;
  49.             }
  50.             $ms_resp .= $this->send_command($fp, "QUIT");
  51.             fclose($fp);
  52.         }
  53.         return $b_server_found;
  54.     }
  55.  
  56.     private function getMXHost(){
  57.         if(!empty($this->mxhost)){
  58.             return $this->mxhost;
  59.         }
  60.         list($user, $domain) = explode("@", $this->email);
  61.         getmxrr($domain, $hosts, $weights);
  62.         $priority = mt_getrandmax();
  63.         $key      = 0;
  64.         if(empty($weights)){
  65.             return false;
  66.         }
  67.         foreach($weights as $k => $v){
  68.             if($v < $priority){
  69.                 $key      = $k;
  70.                 $priority = $v;
  71.             }
  72.         }
  73.         return $hosts[$key];
  74.     }
  75.  
  76.     private function send_command($fp, $out){
  77.         fwrite($fp, $out . "\r\n");
  78.         return $this->get_data($fp);
  79.     }
  80.  
  81.     private function get_data($fp){
  82.         $s = "";
  83.         stream_set_timeout($fp, 2);
  84.         for($i = 0; $i < 2; $i++){
  85.             $s .=fgets($fp, 1024);
  86.         }
  87.         return $s;
  88.     }
  89.  
  90. }
  91.  
  92. $hosts = array(
  93.     "asdfsfd@adsfasdasd.com",
  94.     "asdfsfd@google.com",
  95.     "asdf!sfd@gmail.com",
  96.     "asdfsfd@yahoo.com",
  97.     "asd^sfd@44n5o$.com",
  98.     "dddasdf@gamil.com"
  99. );
  100.  
  101. foreach($hosts as $host){
  102.     echo $host . "\n";
  103.     $em = new EmailValidator($host);
  104.     echo "    ";
  105.     var_dump($em->simpleCheck());
  106.     echo "    ";
  107.     var_dump($em->strongCheck());
  108. }
  109.  
  110.  

回复 "php对Email邮箱地址进行邮件验证强验证"

这儿你可以回复上面这条便签

captcha