How to Validate Email Address in PHP

Check if an email address is correct and really exists using PHP

Email Validation in a Nutshell

An Email Address can look right but still be wrong and bounce. MailValidation.io uses in depth email address validation to check if emails really exist without sending any messages.

Checking Formatting with filter_var

You can use Regular Expressions to check if an Email Address is formatted correctly. In PHP there is the built in regex, FILTER_VALIDATE_EMAIL

				
					// "not an email" is invalid so its false.
php > var_export(filter_var("not an email", FILTER_VALIDATE_EMAIL));
false
// "foo@a.com" looks like an email, so it passes even though its not real.
php > var_export(filter_var("foo@a.com", FILTER_VALIDATE_EMAIL));
'foo@a.com'
// "foo@gmail.com" passes, gmail is a valid email server,
//  but gmail require more than 3 letters for the address.
var_export(filter_var("foo@gmail.com", FILTER_VALIDATE_EMAIL));
'foo@gmail.com'
				
			

You could use it in your code like..

				
					if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  // email matches the pattern
} else {
  // email doesnt match the pattern
}
				
			

Email Validation Using an API Key

With an API key you wont be limited by how many addresses you can check or how fast you can check them.

				
					$email = "foo@bar.com";
$api_key = ???;

$content = json_encode(['email' => $email]);

$request_context = stream_context_create(array(
    'method' => 'POST',
    'body' => $content
    'http' => array(
        'header'  => "Authorization: Api-Key " . $api_key
    )
));

$result_json = file_get_contents("https://app.mailvalidation.io/a/{team_slug}/validate/api/validate/", false, $request_context);

if (json_decode($result_json, true)['is_valid'] == "true") {
	echo("email is valid");
} else {
	echo("email is invalid");
} 
				
			

Support

Are you having trouble, have some questions. How can we help?