How to validate an email address with PHP

Validating an email address is a great way to reduce spam on your website, but there are several methods to do it. You could use a messy regex approach or alternatively you could also use PHP built in functions, it's really up to you. Here is the function I use:

function validEmail($email){
// Check the formatting is correct
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
return FALSE;

// Next check the domain is real.
$domain = explode("@", $email, 2);
return checkdnsrr($domain[1]); // returns TRUE/FALSE;

// Example
validEmail('[email protected]'); // Returns TRUE
validEmail('[email protected]'); // Returns FALSE
View on Github

Free feel to copy/share.

jekyll build