• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

php对Email(邮箱地址)进行强验证的代码

PHP 水墨上仙 2841次浏览

下面的php代码可以对电子邮件地址进行简单验证和强验证,简单验证验证邮件格式和主机是否存在,强验证会连接邮件服务器进行验证,需要比较长时间

<?php 

/* 
 *  __construct($email)     takes an email address to check 
 *  
 * simpleCheck()            Tests to see if an email address is formatted correctly 
 *                          and the domain it belongs to exists, such as: gmail.com, yahoo.com 
 *  
 * strongCheck()            Tests to see if an email address is valid and that the 
 *                          email actually accepts emails by actually connecting to the server. 
 *                          Note: strongCheck() can be slow 
 */ 

class EmailValidator{ 

    private $email  = ""; 
    private $mxhost = ""; 

    public function __construct($email){ 
        $this->email  = $email; 
        $this->mxhost = $this->getMXHost(); 
    } 

    public function strongCheck(){ 
        if(filter_var($this->email, FILTER_VALIDATE_EMAIL) && $this->fConnect()){ 
            return true; 
        } 
        return false; 
    } 

    public function simpleCheck(){ 
        if(filter_var($this->email, FILTER_VALIDATE_EMAIL) && $this->getMXHost()){ 
            return true; 
        } 
        return false; 
    } 

    private function fConnect(){ 
        $fp             = @fsockopen($this->mxhost, 25, $errno, $errstr, 5); 
        $ms_resp        = ""; 
        $b_server_found = false; 
        if($fp){ 
            $ms_resp .= $this->send_command($fp, "HELO hi"); 
            $ms_resp .= $this->send_command($fp, "MAIL FROM:<ryan@ryannaddy.com>"); 
            $rcpt_text = $this->send_command($fp, "RCPT TO:<{$this->email}>"); 
            $ms_resp .= $rcpt_text; 
            if(substr($rcpt_text, 0, 3) == "250"){ 
                $b_server_found = true; 
            } 
            $ms_resp .= $this->send_command($fp, "QUIT"); 
            fclose($fp); 
        } 
        return $b_server_found; 
    } 

    private function getMXHost(){ 
        if(!empty($this->mxhost)){ 
            return $this->mxhost; 
        } 
        list($user, $domain) = explode("@", $this->email); 
        getmxrr($domain, $hosts, $weights); 
        $priority = mt_getrandmax(); 
        $key      = 0; 
        if(empty($weights)){ 
            return false; 
        } 
        foreach($weights as $k => $v){ 
            if($v < $priority){ 
                $key      = $k; 
                $priority = $v; 
            } 
        } 
        return $hosts[$key]; 
    } 

    private function send_command($fp, $out){ 
        fwrite($fp, $out . "\r\n"); 
        return $this->get_data($fp); 
    } 

    private function get_data($fp){ 
        $s = ""; 
        stream_set_timeout($fp, 2); 
        for($i = 0; $i < 2; $i++){ 
            $s .=fgets($fp, 1024); 
        } 
        return $s; 
    } 

} 

$hosts = array( 
    "asdfsfd@adsfasdasd.com", 
    "asdfsfd@google.com", 
    "asdf!sfd@gmail.com", 
    "asdfsfd@yahoo.com", 
    "asd^sfd@44n5o$.com", 
    "dddasdf@gamil.com" 
); 

foreach($hosts as $host){ 
    echo $host . "\n"; 
    $em = new EmailValidator($host); 
    echo "    "; 
    var_dump($em->simpleCheck()); 
    echo "    "; 
    var_dump($em->strongCheck()); 
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明php对Email(邮箱地址)进行强验证的代码
喜欢 (0)
加载中……