Home  >  Article  >  Backend Development  >  php应验邮箱是否真实存在

php应验邮箱是否真实存在

WBOY
WBOYOriginal
2016-06-13 13:17:11870browse

php验证邮箱是否真实存在
大家好,我是php新手,现在有个问题想请教大家,就是注册时验证该email地址是否真的存在,谢谢 

环境win32+php5.45

test.php

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php echo "<center><br>检查电子邮件地址的正确性:<br>"; 
require("email_validation.php"); 
$newmail = "283285993@qq.com"; 
$validator=new email_validation_class; 
$validator->timeout=10; 

//if(IsSet($newemail) && strcmp($newemail,"")){
    if( ($result=$validator->ValidateEmailBox($newmail) )"; 
        return; 
    }else{ 
            echo "22222222";
        if(!$result){ 
            echo "您输入的信箱地址是不正确的! :)<br>"; 
            return; 
        }else{
            echo "邮箱合法!<br>"; 
        } 
    }

?>




email_validation.php

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
 
class email_validation_class 
{ 
//var $email_regular_expression="^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$"; 
var $timeout=0; 
var $localhost=""; 
var $localuser=""; 
var $hosts=0;

Function GetLine($connection) 
{ 
for($line="";;) 
{ 
if(feof($connection)) 
return(0); 
$line.=fgets($connection,100); 
$length=strlen($line); 
if($length>=2 && substr($line,$length-2,2)=="/r/n") 
return(substr($line,0,$length-2)); 
} 
} 

Function PutLine($connection,$line) 
{ 
return(fputs($connection,"$line/r/n")); 
} 

Function ValidateEmailAddress($email) { 
//return(eregi($this->email_regular_expression,$email)!=0); 
//origin:
//return(eregi("^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$",$email)!=0); 

//return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_REQUEST[$email]);
return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $_REQUEST[$email]);

} 

Function ValidateEmailHost($email,$hosts=0) 
{ 
if(!$this->ValidateEmailAddress($email)) 
return(0); 
$user=strtok($email,"@"); 
$domain=strtok(""); 
if(GetMXRR($domain,$hosts,$weights)) 
{ 
$mxhosts=array(); 
for($host=0;$host<count ksort for else if return function verifyresultlines while>GetLine($connection))) 
{ 
if(!strcmp(strtok($line," "),$code)) 
return(1); 
if(strcmp(strtok($line,"-"),$code)) 
return(0); 
} 
return(-1); 
} 

Function ValidateEmailBox($email) 
{ 
if(!$this->ValidateEmailHost($email,$hosts)) 
return(0); 
if(!strcmp($localhost=$this->localhost,"") && !strcmp($localhost=getenv("SERVER_NAME"),"") && !strcmp($localhost=getenv("HOST"),"")) 
$localhost="localhost"; 
if(!strcmp($localuser=$this->localuser,"") && !strcmp($localuser=getenv("USERNAME"),"") && !strcmp($localuser=getenv("USER"),"")) 
$localuser="root"; 
for($host=0;$host<count if>timeout ? fsockopen($hosts[$host],25,$errno,$error,$this->timeout) : fsockopen($hosts[$host],25)))) 
{ 
if($this->VerifyResultLines($connection,"220")>0 && $this->PutLine($connection,"HELO $localhost") && $this->VerifyResultLines($connection,"250")>0 && $this->PutLine($connection,"MAIL FROM: ") && $this->VerifyResultLines($connection,"250")>0 && $this->PutLine($connection,"RCPT TO: ") && ($result=$this->VerifyResultLines($connection,"250"))>=0) 
{ 
fclose($connection); 
return($result); 
} 
fclose($connection); 
} 
} 
return(-1); 
} 
}; 

?> 


 <div class="clear">
                 
              
              
        
            </div></count></count>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn