Hi,
Validating an email address is not a big deal. Conventionally we use two steps to validate an email address:
Step 1. Just use regular expressions to check whether an email address follows the correct pattern.
Step 2. Then you just send the an email to that email address with a activation link to make sure that the email is valid.
But you can do I more check in between Step 1 and 2, just to make things more sensible. Here is a small mockup code for doing that:
<?php
function validate_domain($email){
$strpos=strpos($email,"@");
$domain=substr($email,$strpos + 1);
if(!@file_get_contents("http://".$domain) && !@file_get_contents("https://".$domain)){
echo $email." Status: <font color='red'>Invalid email domain</font><br/>";
}else{
echo $email." Status: <font color='green'>Valid email domain</font></br>";
}
}
validate_domain("xyz@gmail.com");
validate_domain("xyz@incorrectdomainname.com");
?>
Explanation:
The idea is very simple. Just extract the domain name of the email address and make an http call via CURL or file_get_contents(). I have used file_get_contents() to make things simple to understand, but you can replace it by CURL, if needed. If the response of the call is valid that mean the domain is correct, but if the file_get_contents() returns a fatal error, then the domain is invalid.
i.e.
I have checked with both http and https, to stay on the safe side. Even if the email is a POP3 type. Still file_get_contents() will not return fatal error. An error will be returned only and only if the domain is invalid and does not exists.
After executing the above code, this is what I get:
Post in comments for better solution :)
Validating an email address is not a big deal. Conventionally we use two steps to validate an email address:
Step 1. Just use regular expressions to check whether an email address follows the correct pattern.
Step 2. Then you just send the an email to that email address with a activation link to make sure that the email is valid.
But you can do I more check in between Step 1 and 2, just to make things more sensible. Here is a small mockup code for doing that:
<?php
function validate_domain($email){
$strpos=strpos($email,"@");
$domain=substr($email,$strpos + 1);
if(!@file_get_contents("http://".$domain) && !@file_get_contents("https://".$domain)){
echo $email." Status: <font color='red'>Invalid email domain</font><br/>";
}else{
echo $email." Status: <font color='green'>Valid email domain</font></br>";
}
}
validate_domain("xyz@gmail.com");
validate_domain("xyz@incorrectdomainname.com");
?>
Explanation:
The idea is very simple. Just extract the domain name of the email address and make an http call via CURL or file_get_contents(). I have used file_get_contents() to make things simple to understand, but you can replace it by CURL, if needed. If the response of the call is valid that mean the domain is correct, but if the file_get_contents() returns a fatal error, then the domain is invalid.
i.e.
if(!@file_get_contents("http://".$domain) && !@file_get_contents("https://".$domain)){
echo $email." Status: <font color='red'>Invalid email domain</font><br/>";
}else{
echo $email." Status: <font color='green'>Valid email domain</font></br>";
}
I have checked with both http and https, to stay on the safe side. Even if the email is a POP3 type. Still file_get_contents() will not return fatal error. An error will be returned only and only if the domain is invalid and does not exists.
After executing the above code, this is what I get:
Post in comments for better solution :)
No comments:
Post a Comment