Email validation with PHP

Tags: , ,

This script is more accurate then a normal validation script. After checking if the email is correctly typed in (@) it checks the domain validity by checking it’s dns records (MX, A and CNAME). A piece of the regex is cut because of my sidebar but if you copy/paste everything you should see it :)

function check_email($email) {
	if( (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $email)) ||
 		(preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',$email)) ) {
 		$host = explode('@', $email);
 		if(checkdnsrr($host[1].'.', 'MX') ) return true;
 		if(checkdnsrr($host[1].'.', 'A') ) return true;
 		if(checkdnsrr($host[1].'.', 'CNAME') ) return true;
 	}
 	return false;
}
If you liked this post think about subscribing to my RSS feed and prevent missing anything interesting. It's free, fast and doesn't hurt. Promise. Click here.
Related posts: