Most of people who are new to php world, just verify if the text box of the email address is filled or not, without verifying the syntax of the email address...
Something like this they use:
But when you want to develop a php website in the best way, taking care of all little details, you should think what happens when your user does make a mistake in the email address, writing something like this: his_email@gmailcom, or his_emailgmail.com or even he writes ssdfsff (like many people do), I'm sure you would like to inform him and ask him to correct his email address, you maybe also want to check if the domain name of his email address is valid, you can't accept an email like this: blahblah@dfsjhbhjjqsdqq.fdf .
So here is the php script I think you should use everytime you have a form with 'email address' input :
function validate_email($email) {
//check for all the non-printable codes in the standard ASCII set,
//including null bytes and newlines, and exit immediately if any are found.
if (preg_match("/[\\000-\\037]/",$email)) {
return false;
}
$pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD";
if(!preg_match($pattern, $email)){
return false;
}
// Validate the domain exists with a DNS check
// if the checks cannot be made (soft fail over to true)
list($user,$domain) = explode('@',$email);
if( function_exists('checkdnsrr') ) {
if( !checkdnsrr($domain,"MX") ) { // Linux: PHP 4.3.0 and higher & Windows: PHP 5.3.0 and higher
return false;
}
}
else if( function_exists("getmxrr") ) {
if ( !getmxrr($domain, $mxhosts) ) {
return false;
}
}
return true;
} // end function validate_email
?>
Now you can use this function with the email address you want to verify as parameter, and it will return you true if it valide and false if it is not.
This nice function will not know if an email exist really or not, it just verifies the syntax and the domain name, for example will not know that qsddsfqsdsdfqsdsqdsqd@gmail.com is not valide, but knows that qsqsdsq@dfsfsqdqsdqd.com is an invalid email address .
I hope this help you a lot.
Please leave comments or link to us if you think this is usefull :)