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.