How to validate email addresses in PHP

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

You can easily test if an email address is formatted correctly with a regex, like in the example bellow. But it will miss many important cases, like if the email address is well formed but the emails will bounce, because the account never existed or has recently been disabled.

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

Real email API validation

To do full email validation inspecting the mail server you can use isitarealemail.com and make a requests call to check it. This will do the check via the real email service which will make sure the requests are not blocked.

validate_email.php
$email = "foo@bar.com";
$api_key = ???;
$request_context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Bearer " . $api_key
)
));
$result_json = file_get_contents("https://isitarealemail.com/api/email/validate?email=" . $email, false, $request_context);
if (json_decode($result_json, true)['status'] == "valid") {
echo("email is valid");
} else {
echo("email is not valid");
}

Be sure to signup and get your own API key.

Get started with real email validations today.