PHP Code:
<?php
/*
* email_validation.php
*
* @(#) $Header: /cvsroot/PHPlibrary/email_validation.php,v 1.9 2000/09/25 03:28:15 mlemos Exp $
*
*/
class email_validation_class
{
var $email_regular_expression="^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~ ])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~ ]+\\.)+[a-zA-Z]{2,4}\$";
var $timeout=0;
var $localhost="";
var $localuser="";
var $debug=0;
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
&& 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=strtok($email,"@");
$domain=strtok("");
if(@GetMXRR($domain,&$hosts,&$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 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($hosts);$host++)
{
if($this->debug)
$this->OutputDebug("Connecting to host \"".$hosts[$host]."\"...");
if(($connection=($this->timeout ? fsockopen($hosts[$host],25,&$errno,&$error,$this->timeout) : fsockopen($hosts[$host],25))))
{
if($this->debug)
$this->OutputDebug("Connected.");
if($this->VerifyResultLines($connection,"220")>0
&& $this->PutLine($connection,"HELO $localhost")
&& $this->VerifyResultLines($connection,"250")>0
&& $this->PutLine($connection,"MAIL FROM: <$localuser@$localhost>")
&& $this->VerifyResultLines($connection,"250")>0
&& $this->PutLine($connection,"RCPT TO: <$email>")
&& ($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);
}
};
?>
The above is a class (not written by me, its fully GPLed tho, with a wee modification by me on line 59 so it works on a Win32 server) which should help you do a more advanced form of email authentication. The first function, ValidateEmailAddress, does what the person above has suggested. The next function ValidateEmailHost checks that the bit after the @ sign (eg. hotmail.com) is a registered domain name that points to a server. Then ValidateEmailBox actually makes a connection to the server and checks that the individual mailbox exists.
Personally I would go for the second method (which calls the first within it btw) as the 3rd method won't work for web based email accounts such as hotmail which don't have a POP server.
Heres an example of how to use it:
PHP Code:
<?php
$validator = new email_validation_class;
if(!$validator->ValidateEmailBox($email))
{
echo("Not a valid email address!");
exit();
}
else
{
echo("Woo yeah, Email valid");
}
?>
Enjoy
Bookmarks