More Email Validation

Submitted by:Andery Smith

Date added:26 December, 2012

Category:PHP

Email regex validation with MX check and communication. Sample code included. Returns bool valid code, bool condition code and str message.

Tags: email validation

Code Snippet:

<?php

//sample code
//$eMail = "troy@churchgrowthweb.com";
//$IsItValid = validateEmail($eMail);
//echo "<p>";
//$valid = (!$IsItValid[0]) ? "FALSE" : "TRUE";
//echo $eMail." is a valid email address: ".$valid."<br>\n";
//$spcmsg = (!$IsItValid[1]) ? "FALSE" : "TRUE";
//echo "Special validation condition exists: ".$spcmsg."<br>\n";
//$strmsg = $IsItValid[2];
//echo "Returned validation message: ".$strmsg."<br>\n";
//echo "</p>\n";
//exit;

/*
Function: validateEmail()
Date: 02/03/2003
Usage: validateEmail(email_address,<$DEBUG=true>)
Returns: array $Return
$Return[0]: int 0 = invalid email
int 1 = valid email
$Return[1]: int 0 = no special validation condition
int 1 = special validation condition exists
$Return[2]: str 'text of return message'
Platforms: Unix,Linux
Versions: PHP3, PHP4
Notes:
- Special Validation Conditions return TRUE based on criteria which
make it impossible to validate an email address against a mail server.
For example, these conditions may exist if MX records are munged or
returned in an unexpected format, or if there is a system error
preventing the lookup and extraction of the MX server, or if the
answering mail server just refuses permission to logon (AOL mostly).
*/
function validateEmail($eMail,$DEBUG=false) {
if (!$DEBUG) error_reporting(0);
global $HTTP_HOST;
$myhost = $HTTP_HOST;
if ($DEBUG) echo "Checking format of ".$eMail.".<br>\n";
//verify email address format
if (!eregi("^[_a-z0-9_-]+(\.[_a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)*(\.[a-z]{2,3})$", $eMail)) {
//return with an invalid address error
$returnMsg = $eMail." is an invalid email address format.<br><br>\n";
if ($DEBUG) echo $returnMsg;
$Return[0] = FALSE;
$Return[1] = FALSE;
$Return[2] = $returnMsg;
return $Return;
} else {
if ($DEBUG) echo "Format of ".$eMail." verified.<br><br>\n";
}
list($userName,$domainName ) = split ("@",$eMail);
$domainName = $domainName.".";
if (checkdnsrr($domainName,"MX")) {
if (getmxrr($domainName,$mxRecord)) {
//save the MX hostname
$mailSvr = $mxRecord[0];
} else {
//MX record exists but failed to retrieve
//return with a system error message NOT an invalid address
//set special condition flag $Return[1]
$returnMsg = "System error - problem with MX record lookup. Email address may be valid.";
if ($DEBUG) echo $returnMsg;
$Return[0] = TRUE;
$Return[1] = TRUE;
$Return[2] = $returnMsg;
return $Return;
}
} else {
//no MX record found, assume $domainName is also the mail server
$mailSvr = $domainName;
}

if ($DEBUG) echo "Using mail server ".$mailSvr."...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>\n";
if ($DEBUG) echo "Trying ".$mailSvr." on SMTP port 25...<br><br>\n";
$Connection = fsockopen($mailSvr, 25, $errno, $errstr, 10);
if ($Connection) {
$InitConnect = fgets($Connection, 1024);
if ($DEBUG) echo "Connection Made to ".$mailSvr.".<br><br>\n";
if ($DEBUG) echo $mailSvr."> ".$InitConnect."<br><br>\n";
//start the SMTP validation
if (ereg("^220",$InitConnect)) {
//it's a friendly SMTP server, start communication
//store the response
$mailOpenQ = "HELO " .$myhost. "\r\n";
fputs($Connection, "$mailOpenQ");
$mailOpenA = fgets($Connection, 1024);
if ($DEBUG) echo $myhost."> ".$mailOpenQ."<br>\n";
if ($DEBUG) echo $mailSvr."> ".$mailOpenA."<br><br>\n";

//Ask it to accept mail from phpemailtester@$myhost
//store the response
$mailFromQ = "MAIL FROM: <phpemailtester@$myhost>\r\n";
fputs($Connection, "$mailFromQ");
$mailFromA = fgets($Connection,1024);
if ($DEBUG) echo $myhost."> ".htmlspecialchars($mailFromQ)."<br>\n";
if ($DEBUG) echo $mailSvr."> ".$mailFromA."<br><br>\n";

//Ask it to accept mail for delivery to $eMail user
//store the response
$mailToQ = "RCPT TO: <$eMail>\r\n";
fputs($Connection, "$mailToQ");
$mailToA = fgets($Connection,1024);
if ($DEBUG) echo $myhost."> ".htmlspecialchars($mailToQ)."<br>\n";
if ($DEBUG) echo $mailSvr."> ".$mailToA."<br><br>\n";

//Close the connection
$mailQuit = "QUIT\r\n";
fputs ($Connection, $mailQuit);
socket_set_timeout($Connection,1,1);
fclose($Connection);
if ($DEBUG) echo $myhost."> ".$mailQuit."<br><br>\n";

//look for a "250" response in $mailTo
if (ereg("^250", $mailToA)) {
// return with a successful validation
$returnMsg = $mailSvr." replied that ".$eMail." exists or is, at least, syntactically correct.<br>\n";
if ($DEBUG) echo $returnMsg;
$Return[0] = TRUE;
$Return[1] = FALSE;
$Return[2] = $returnMsg;
return $Return;
} else {
// the server refused the user so return with invalid address error
$returnMsg = $mailSvr." returned bad or non-existent address error.";
if ($DEBUG) echo $returnMsg;
$Return[0] = FALSE;
$Return[1] = FALSE;
$Return[2] = $returnMsg;
return $Return;
}
} else if (ereg("^550)", $InitConnect)) {
//connected to an unfriendly mail server or received an error response
//most likley from AOL, must accept $eMail as valid
//set special condition flag $Return[1]
if (ereg("aol.com$", $eMail) || ereg("AOL", $InitConnect)) {
$returnMsg = "Unfriendly AOL mailserver (".$mailSvr.") - cannot verify ".$eMail.".<br>\n";
} else {
$returnMsg = "Unfriendly mail server (".$mailSvr.") will not let us verify ".$eMail.".<br>\n";
}
//attempt to close nicely anyway, if needed
fputs ($Connection, "QUIT\r\n");
fclose($Connection);

if ($DEBUG) echo $returnMsg;
$Return[0] = TRUE;
$Return[1] = TRUE;
$Return[2] = $returnMsg;
return $Return;
} else {
//connection made, but it failed to identfy itself as an SMTP server
//so return with an invalid address error
$returnMsg = "Connected to ".$mailSvr.", but does not appear to be a mail server. Address verification fails.";
//attempt to close nicely, if needed
fputs ($Connection, "QUIT\r\n");
fclose($Connection);

if ($DEBUG) echo $returnMsg;
$Return[0] = FALSE;
$Return[1] = FALSE;
$Return[2] = $returnMsg;
return $Return;
}
} else {
// the connection failed - return with an invalid address error
$returnMsg = "Connection to ".$mailSvr." failed. Address verification fails.";
if ($DEBUG) echo $returnMsg;
$Return[0] = FALSE;
$Return[1] = FALSE;
$Return[2] = $returnMsg;
return $Return;
}
}
?>
 
 

Comments