Showing posts with label email domain. Show all posts
Showing posts with label email domain. Show all posts

Thursday, March 24, 2011

Checking valid email domain - PHP

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: