用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

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

2014-12-03 作者: php源代码大全举报

[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()); 
}



网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...