How to validate email addresses in PHP

Let’s look at a simple way to both check the syntax of an email address and determine if it really exists.

First, add this reference to our Validate API to your library.

"require": {
"cloudmersive/cloudmersive_validate_api_client": "^1.4",
}

Now call emailFullValidation

<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\EmailApi(


new GuzzleHttp\Client(),
$config
);
$email = "email_example"; // string | Email address to validate, e.g. \"support@cloudmersive.com\". The input is a string so be sure to enclose it in double-quotes.
try {
$result = $apiInstance->emailFullValidation($email);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EmailApi->emailFullValidation: ', $e->getMessage(), PHP_EOL;
}
?>

And here are what the results will look like:

{
"ValidAddress": true,
"MailServerUsedForValidation": "mx.zoho.com"
}

This method is highly effective as it checks the syntax before connecting with the server, then returns the answer as well as the server’s name.

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store