全面測試email的有效性

2000-12-18 10:35:42  作者
一般我們常希望拜訪你的網站的朋友能留下Email
但是很多人都會隨便打,造成治理員的困擾,
以下這個class可以線上檢查Email是否是有效的Email(存不存在)

%26lt;?
class CEmail {
var $email_regular_expression=%26quot;^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$%26quot;;
var $timeout=0;
var $localhost=%26quot;%26quot;;
var $localuser=%26quot;%26quot;;

Function GetLine($connection)
{
for($line=%26quot;%26quot;;;)
{
if(feof($connection))
return(0);
$line.=fgets($connection,100);
$length=strlen($line);
if($length%26gt;=2
%26amp;%26amp; substr($line,$length-2,2)==%26quot;rn%26quot;)
return(substr($line,0,$length-2));
}
}

Function PutLine($connection,$line)
{
return(fputs($connection,%26quot;$linern%26quot;));
}

Function VerifyRule($email)
{
return(eregi($this-%26gt;email_regular_expression,$email)!=0);
}

Function ValidateEmailHost($email,$hosts=0)
{
if(!$this-%26gt;VerifyRule($email))
return(0);
$user=strtok($email,%26quot;@%26quot;);
$domain=strtok(%26quot;%26quot;);
if(GetMXRR($domain,%26amp;$hosts,%26amp;$weights))
{
$mxhosts=array();
for($host=0;$host%26lt;count($hosts);$host++)
$mxhosts[$weights[$host]]=$hosts[$host];
KSort($mxhosts);
for(Reset($mxhosts),$host=0;$host%26lt;count($mxhosts);Next($mxhosts),$host++)
$hosts[$host]=$mxhosts[Key($mxhosts)];
}
else
{
$hosts=array();
if(strcmp(@gethostbyname($domain),$domain)!=0)
$hosts[]=$domain;
}
return(count($hosts)!=0);
}

Function VerifyResultLines($connection,$code)
{
while(($line=$this-%26gt;GetLine($connection)))
{
if(!strcmp(strtok($line,%26quot; %26quot;),$code))
return(1);
if(strcmp(strtok($line,%26quot;-%26quot;),$code))
return(0);
}
return(-1);
}

Function VerifyOnline($email)
{
if(!$this-%26gt;ValidateEmailHost($email,%26amp;$hosts))
return(0);
if(!strcmp($localhost=$this-%26gt;localhost,%26quot;%26quot;)
%26amp;%26amp; !strcmp($localhost=getenv(%26quot;SERVER_NAME%26quot;),%26quot;%26quot;)
%26amp;%26amp; !strcmp($localhost=getenv(%26quot;HOST%26quot;),%26quot;%26quot;))
$localhost=%26quot;localhost%26quot;;
if(!strcmp($localuser=$this-%26gt;localuser,%26quot;%26quot;)
%26amp;%26amp; !strcmp($localuser=getenv(%26quot;USERNAME%26quot;),%26quot;%26quot;)
%26amp;%26amp; !strcmp($localuser=getenv(%26quot;USER%26quot;),%26quot;%26quot;))
$localuser=%26quot;root%26quot;;
for($host=0;$host%26lt;count($hosts);$host++)
{
if(($connection=($this-%26gt;timeout ? fsockopen($hosts[$host],25,%26amp;$errno,%26amp;$error,$this-%26gt;timeout) : fsockopen($hosts[$host],25))))
{
if($this-%26gt;VerifyResultLines($connection,%26quot;220%26quot;)%26gt;0
%26amp;%26amp; $this-%26gt;PutLine($connection,%26quot;HELO $localhost%26quot;)
%26amp;%26amp; $this-%26gt;VerifyResultLines($connection,%26quot;250%26quot;)%26gt;0
%26amp;%26amp; $this-%26gt;PutLine($connection,%26quot;MAIL FROM: %26lt;$localuser@$localhost%26gt;%26quot;)
%26amp;%26amp; $this-%26gt;VerifyResultLines($connection,%26quot;250%26quot;)%26gt;0
%26amp;%26amp; $this-%26gt;PutLine($connection,%26quot;RCPT TO: %26lt;$email%26gt;%26quot;)
%26amp;%26amp; ($result=$this-%26gt;VerifyResultLines($connection,%26quot;250%26quot;))%26gt;=0)
{
fclose($connection);
return($result);
}
fclose($connection);
}
}
return(-1);
}

function Verify($email,$type=0) {
if($type==0) return $this-%26gt;VerifyRule($email) ;
elsereturn $this-%26gt;VerifyOnline($email) ;

}

};


?%26gt;


用法:
$m=new CEmail;
//僅檢查語法
if($m-%26gt;Verify(%26quot;jerry@mail.jerry.com.tw%26quot;,0)) echo %26quot;有效%26quot;;
else echo %26quot;無效%26quot;;

//線上檢查是否真的有該Email
if($m-%26gt;Verify(%26quot;jerry@mail.jerry.com.tw%26quot;,1)) echo %26quot;有效%26quot;;
else echo %26quot;無效%26quot;;