Hi Guys
Today we are going to discuss about "How to verify email in PHP". I read many articles regarding this issue.
There is no perfect way that we can find out whether the email id is exist or not, but we can find out the existence of email domain.
So, I want to share my knowledge with you.
For a format validation we can use regular expression like
^[a-z0-9\.]+@[a-z0-9]+\.[a-z]{2,6}$
In above pattern. I define that email can be alphanumeric a-zA-Z and can be dot, numeric 0-9 and dot \. After that for domain also been checked same way as ID excluded dot so i had excluded.
So, extension also which can be alphabets from 2 to 6 character.
To check the format we can use this regular expression in php like below.
$txtemail='someone@domain.com';
if(!ereg('^[a-zA-Z0-9\.]+@[a-zA-Z0-9]+\.[a-z]{2,6}$',$txtemail))
{
echo 'Invalid Email';
}else{
//check for email domain
}
In above else parts we can confirm that the email format is Okay but domain may not be exist. So, to check that we can do the following things.
In window system, we can use one dos command "nslookup" to check the domain. For example
nslookup -type=MX gmail.com
output:
gmail.com mx preference = 20, mail exchange =
alt2.gmail-smtp-in.l.google.com
.................
.................
It means it is valid. Otherwise it will return some error.
We can execute this command using exec() function like exec('nslookup -type=MX gmail.com');
In this way at some extent we can check the validity of email.
Thanks for giving yours time. Happy coding day....
No comments:
Post a Comment