一般我&;#20497;常希望拜&;#35370;你的&;#32178;站的朋友能留下Email
但是很多人都&;#26371;&;#38568;便打,造成管理&;#21729;的困&;#25854;,
以下&;#36889;&;#20491;class可以&;#32218;上&;#27298;查Email是否是有效的Email(存不存在)
<?
class CEmail {
var $email_regular_expression="^([a-z0-9_]│\-│\.) @(([a-z0-9_]│\-) \.) [a-z]{2,4}$";
var $timeout=0;
var $localhost="";
var $localuser="";
Function GetLine($connection)
{
for($line="";;)
{
if(feof($connection))
return(0);
$line.=fgets($connection,100);
$length=strlen($line);
if($length>=2
&;amp;&;amp; substr($line,$length-2,2)=="rn")
return(substr($line,0,$length-2));
}
}
Function PutLine($connection,$line)
{
return(fputs($connection,"$linern"));
}
Function VerifyRule($email)
{
return(eregi($this->email_regular_expression,$email)!=0);
}
Function ValidateEmailHost($email,$hosts=0)
{
if(!$this->VerifyRule($email))
return(0);
$user=strtok($email,"@");
$domain=strtok("");
if(GetMXRR($domain,&;amp;$hosts,&;amp;$weights))
{
$mxhosts=array();
for($host=0;$host<count($hosts);$host )
$mxhosts[$weights[$host]]=$hosts[$host];
KSort($mxhosts);
for(Reset($mxhosts),$host=0;$host<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->GetLine($connection)))
{
if(!strcmp(strtok($line," "),$code))
return(1);
if(strcmp(strtok($line,"-"),$code))
return(0);
}
return(-1);
}
Function VerifyOnline($email)
{
if(!$this->ValidateEmailHost($email,&;amp;$hosts))
return(0);
if(!strcmp($localhost=$this->localhost,"")
&;amp;&;amp; !strcmp($localhost=getenv("SERVER_NAME"),"")
&;amp;&;amp; !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
if(!strcmp($localuser=$this->localuser,"")
&;amp;&;amp; !strcmp($localuser=getenv("USERNAME"),"")
&;amp;&;amp; !strcmp($localuser=getenv("USER"),""))
[color=#FFFFFF'][/color]
$localuser="root";
for($host=0;$host<count($hosts);$host )
{
if(($connection=($this->timeout ? fsockopen($hosts[$host],25,&;amp;$errno,&;amp;$error,$this->timeout) : fsockopen($hosts[$host],25))))
{
if($this->VerifyResultLines($connection,"220")>0
&;amp;&;amp; $this->PutLine($connection,"HELO $localhost")
&;amp;&;amp; $this->VerifyResultLines($connection,"250")>0
&;amp;&;amp; $this->PutLine($connection,"MAIL FROM: <$localuser@$localhost>")
&;amp;&;amp; $this->VerifyResultLines($connection,"250")>0
&;amp;&;amp; $this->PutLine($connection,"RCPT TO: <$email>")
&;amp;&;amp; ($result=$this->VerifyResultLines($connection,"250"))>=0)
{
fclose($connection);
return($result);
}
fclose($connection);
}
}
return(-1);
}
function Verify($email,$type=0) {
if($type==0) return $this->VerifyRule($email) ;
else return $this->VerifyOnline($email) ;
}
};
?>
用法:
$m=new CEmail;
//&;#20677;&;#27298;查&;#35486;法
if($m->Verify("
jerry@mail.jerry.com.tw",0)) echo "有效";
else echo "&;#28961;效";
//&;#32218;上&;#27298;查是否真的有&;#35442;Email
if($m->Verify("
jerry@mail.jerry.com.tw",1)) echo "有效";
else echo "&;#28961;效";