当前位置:Linux教程 - Email - 判断一个email是否存在的类

判断一个email是否存在的类

[转贴]判断一个Email是否存在的类

判断一个Email是否存在的类
作者:mlemos
来源:www.fastboard.org

<?
/*
* email_validation.php
*
* @(#) $Header: /home/mlemos/cvsroot/PHPlibrary/email_validation.php,v 1.10 2002/01/12 02:39:57 mlemos Exp $
*
*/

class email_validation_class
{
var
$email_regular_expression="^([-!#$%&amp;'*+./0-9=?A-Z^_`a-z{ | }~])[email protected]([-!#$%&amp;'*+/0-9=?A-Z^_`a-z{ | }~]+.)+[a-zA-Z]{ 2,4 }$";
var
$timeout=0;
var
$localhost="";
var
$localuser="";
var
$debug=0;
var
$next_token="";

Function
Tokenize($string,$separator="")
{
if(!
strcmp($separator,""))
{
$separator=$string;
$string=$this->next_token;
}
for(
$character=0;$character<strlen($separator);$character++)
{
if(
GetType($position=strpos($string,$separator[$character]))=="integer")
$found=(IsSet($found) ? min($found,$position) : $position);
}
if(IsSet(
$found))
{
$this->next_token=substr($string,$found+1);
return(
substr($string,0,$found));
}
else
{
$this->next_token="";
return(
$string);
}
}

Function
OutputDebug($message)
{
echo
$message,"\n";
}

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)=="\r\n")
{
$line=substr($line,0,$length-2);
if(
$this->debug)
$this->OutputDebug("< $line");
return(
$line);
}
}
}

Function
PutLine($connection,$line)
{
if(
$this->debug)
$this->OutputDebug("> $line");
return(
fputs($connection,"$line\r\n"));
}

Function
ValidateEmailAddress($email)
{
return(
eregi($this->email_regular_expression,$email)!=0);
}

Function
ValidateEmailHost($email,$hosts=0)
{
if(!
$this->ValidateEmailAddress($email))
return(
0);
$user=$this->Tokenize($email,"@");
$domain=$this->Tokenize("");
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($this->Tokenize($line," "),$code))
return(
1);
if(
strcmp($this->Tokenize($line,"-"),$code))
return(
0);
}
return(-
1);
}

Function
ValidateEmailBox($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"),""))
$localuser="root";
for(
$host=0;$host<count($hosts);$host++)
{
if(
$this->debug)
$this->OutputDebug("Connecting to host \"".$hosts[$host]."\"...");
if((
$connection=($this->timeout ? fsockopen($hosts[$host],25,&amp;$errno,&amp;$error,$this->timeout) : fsockopen($hosts[$host],25))))
{
if(
$this->debug)
$this->OutputDebug("Connected.");
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: <[email protected]$localhost>")
&
amp;&amp; $this->VerifyResultLines($connection,"250")>0
&amp;&amp; $this->PutLine($connection,"RCPT TO: <$email>")
&
amp;&amp; ($result=$this->VerifyResultLines($connection,"250"))>=0)
{
if(
$this->debug)
$this->OutputDebug("This host states that the address is ".($result ? "" : "not ")."valid.");
fclose($connection);
if(
$this->debug)
$this->OutputDebug("Disconnected.");
return(
$result);
}
if(
$this->debug)
$this->OutputDebug("Unable to validate the address with this host.");
fclose($connection);
if(
$this->debug)
$this->OutputDebug("Disconnected.");
}
else
{
if(
$this->debug)
$this->OutputDebug("Failed.");
}

}
return(-
1);
}
};

?>