How to Validate Emails using APIs in PHP

Cloudmersive
3 min readMay 4, 2024

Email address validation is incredibly important for any online form submission application collecting user contact information.

When we go about validating email addresses, we can approach the problem on a few different levels.

  1. We can validate the syntax of the email to ensure it doesn’t contain any obvious errors (e.g., johndoe@cloudmersive,com contains a critical syntax error)
  2. We can check if the parent domain of a syntactically valid email address has email servers defined (e.g., johndoe@cloudmersive.com is syntactically valid, and a quick lookup shows the domain “Cloudmersive” has email servers defined)
  3. We can check if syntactically valid email address domains have email servers defined, AND we can silently ping those servers to validate the existence of the specific account (e.g., johndoe@cloudmersive.com has a valid domain, but “John Doe” is not a valid account).

Conveniently, we can easily take advantage of three free APIs that perform each of the above operations respectively.

We’ll just need to copy from PHP code examples provided below to structure our API call(s), and we’ll be able to validate email address inputs without any trouble at all. We can authorize our requests with a free Cloudmersive API key to make up to 800 API calls per month with no additional commitments.

Our first step is to install the PHP client. We can install with Composer by executing the following command from the command line:

composer require cloudmersive/cloudmersive_validate_api_client

With that out of the way, we can call an API to syntactically validate email addresses using the below code:

<?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
);
$value = "value_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->emailPost($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EmailApi->emailPost: ', $e->getMessage(), PHP_EOL;
}
?>

And we can use the following examples to syntactically validate AND check the domain for defined email servers:

<?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->emailAddressGetServers($email);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EmailApi->emailAddressGetServers: ', $e->getMessage(), PHP_EOL;
}
?>

Lastly, we can use the following code to syntactically validate email addresses, check the domain for defined email servers, AND quietly (i.e., without sending any emails) ping the email servers to validate the user email exists on the server:

<?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;
}
?>

Now we have three quick and easy methods for validating email addresses in PHP!

--

--

Cloudmersive

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