FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Email validation





darence
I have built email field on my webpage, but how to check entered email? Is it possible to check is entered adress valid? If yes, tell me how to do it. Thanks
badai
function getmxrr($hostname, &$mxhosts)
{
$mxhosts = array();
exec('nslookup -type=mx '.$hostname, $result_arr);
foreach($result_arr as $line)
{
if (preg_match("/.*mail exchanger = (.*)/", $line, $matches))
$mxhosts[] = $matches[1];
}
return( count($mxhosts) > 0 );
}

function ValidateMail($Email) {
global $HTTP_HOST;
$result = array();
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {

$result[0]=false;
$result[1]="The e-mail $Email is not properly formatted.";
return $result;
}
list ( $Username, $Domain ) = split ("@",$Email);

if (getmxrr($Domain, $MXHost)) {

$ConnectAddress = $MXHost[0];
} else {

$ConnectAddress = $Domain;

}
error_reporting(0);
$Connect = fsockopen ( $ConnectAddress, 25 );
error_reporting(2047);

if ($Connect) {

if (ereg("^220", $Out = fgets($Connect, 1024))) {

fputs ($Connect, "HELO $HTTP_HOST\r\n");
$Out = fgets ( $Connect, 1024 );
fputs ($Connect, "MAIL FROM: <{$Email}>\r\n");
$From = fgets ( $Connect, 1024 );
fputs ($Connect, "RCPT TO: <{$Email}>\r\n");
$To = fgets ($Connect, 1024);
fputs ($Connect, "QUIT\r\n");
fclose($Connect);
if (!ereg ("^250", $From) ||!ereg ( "^250", $To )) {
$result[0]=false;
$result[1]="Server $ConnectAddress rejected address $Email. Please create e-mail first or check your spelling.";
return $result;

}
} else {

$result[0] = false;
$result[1] = "No response from server $ConnectAddress. Can not validate e-mail address. Please try again later.";
return $result;
}

} else {

$result[0]=false;
$result[1]="Can not connect to E-Mail server $ConnectAddress. Can not validate e-mail address. Please try again later or check your spelling.";
return $result;
}
$result[0]=true;
$result[1]="$Email appears to be valid.";
return $result;
} // end of function

$validemail = 1;
$emailvalidation=ValidateMail($email);
if (!($emailvalidation[0])){
echo $emailvalidation[1];
echo "<br>";
$validemail=0;
}

if ($validemail)
//e-mail is valid
else
//e-mail not valid
MrBlueSky
Readable version of the above:

function getmxrr($hostname, &$mxhosts)
{
$mxhosts = array();
exec('nslookup -type=mx '.$hostname, $result_arr);
foreach($result_arr as $line) {
if (preg_match("/.*mail exchanger = (.*)/", $line, $matches))
$mxhosts[] = $matches[1];
}
return( count($mxhosts) > 0 );
}

function ValidateMail($Email) {
global $HTTP_HOST;
$result = array();
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {

$result[0]=false;
$result[1]="The e-mail $Email is not properly formatted.";
return $result;
}
list ( $Username, $Domain ) = split ("@",$Email);

if (getmxrr($Domain, $MXHost)) {

$ConnectAddress = $MXHost[0];
} else {

$ConnectAddress = $Domain;

}
error_reporting(0);
$Connect = fsockopen ( $ConnectAddress, 25 );
error_reporting(2047);

if ($Connect) {

if (ereg("^220", $Out = fgets($Connect, 1024))) {

fputs ($Connect, "HELO $HTTP_HOST\r\n");
$Out = fgets ( $Connect, 1024 );
fputs ($Connect, "MAIL FROM: <{$Email}>\r\n");
$From = fgets ( $Connect, 1024 );
fputs ($Connect, "RCPT TO: <{$Email}>\r\n");
$To = fgets ($Connect, 1024);
fputs ($Connect, "QUIT\r\n");
fclose($Connect);
if (!ereg ("^250", $From) ||!ereg ( "^250", $To )) {
$result[0]=false;
$result[1]="Server $ConnectAddress rejected address $Email. Please create e-mail first or check your spelling.";
return $result;

}
} else {

$result[0] = false;
$result[1] = "No response from server $ConnectAddress. Can not validate e-mail address. Please try again later.";
return $result;
}

} else {

$result[0]=false;
$result[1]="Can not connect to E-Mail server $ConnectAddress. Can not validate e-mail address. Please try again later or check your spelling.";
return $result;
}
$result[0]=true;
$result[1]="$Email appears to be valid.";
return $result;
} // end of function

$validemail = 1;
$emailvalidation=ValidateMail($email);
if (!($emailvalidation[0])){
echo $emailvalidation[1];
echo "<br>";
$validemail=0;
}

if ($validemail)
//e-mail is valid
else
//e-mail not valid
garionw
My version:

Code:

function checkemail($email){
 if (eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email)) $return = true;
 return $return;
}

if (checkemail($_POST["email"]) == false){
 echo "Invalid";
}
darence
Thanks to all, guys! Very Happy
Related topics
Regestration and other stuff. $FRI up for grabs
subject javascript code please help
Form validation
How to update database?
huge-as code problem on top of le forum
Contact Script
Best Script For Me ?
BOT prevention - stand out from the crowd
how does this email validation works?
Wordpress subdomains not working
Waiting for the email validation...
Email Validation
i need to know
Email Validation script
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.