An extensible email validation library for PHP 7+
The aim of this library is to offer a more detailed email validation report than simply checking if an email is the valid format, and also to make it possible to easily add custom validations.
Currently this tool checks the following:
Validation | Description |
---|---|
MX records | Checks if the email's domain has valid MX records |
Valid format | Validates e-mail addresses against the syntax in RFC 822, with the exceptions that comments and whitespace folding and dotless domain names are not supported (as it uses PHP's filter_var()). |
Email Host | Checks if the email's host (e.g gmail.com) is reachable |
Role/Business Email^ | Checks if the email is a role/business based email (e.g [email protected]). |
Disposable email provider^ | Checks if the email is a disposable email (e.g [email protected]). |
Free email provider^ | Checks if the email is a free email (e.g [email protected]). |
Misspelled Email ^ | Checks the email for possible typos and returns a suggested correction (e.g [email protected] -> [email protected]). |
^ Data used for these checks can be found here
composer require daveearley/daves-email-validation-tool
// Include the composer autoloader
require __DIR__ . '/vendor/autoload.php';
$validator = EmailValidation\EmailValidatorFactory::create('[email protected]');
$jsonResult = $validator->getValidationResults()->asJson();
$arrayResult = $validator->getValidationResults()->asArray();
echo $jsonResult;
Expected output:
{
"valid_format": true,
"valid_mx_records": false,
"possible_email_correction": "[email protected]",
"free_email_provider": false,
"disposable_email_provider": false,
"role_or_business_email": false,
"valid_host": false
}
To add a custom validation simply extend the EmailValidation\Validations\Validator class and implement the getResultResponse() and getValidatorName() methods. You then register the validation using the EmailValidation\EmailValidator->registerValidator() method.
// Validations/GmailValidator.php
<?php
namespace EmailValidation\Validations;
class GmailValidator extends Validator
{
public function getValidatorName(): string
{
return 'is_gmail';
}
public function getResultResponse(): bool
{
$hostName = $this->getEmailAddress()->getHostPart();
return strpos($hostName, 'gmail.com') !== false;
}
}
// file-where-you-are-doing-your-validation.php
<?php
use EmailValidation\Validations\GmailValidator;
require __DIR__ . '/vendor/autoload.php';
$validator = EmailValidation\EmailValidatorFactory::create('[email protected]');
$validator->registerValidator(new GmailValidator());
echo $validator->getValidationResults()->asJson();
The expected output will be:
{
"is_gmail": true,
"valid_format": true,
"valid_mx_records": false,
"possible_email_correction": "",
"free_email_provider": true,
"disposable_email_provider": false,
"role_or_business_email": false,
"valid_host": false
}
You can easily validate over HTTP using docker. To get docker working run
docker-compose up -d
in the repository root. You can then validate an email by navigating to http://localhost:[email protected]. The result will be JSON string as per above.
You can create your own data provider by creating a data provider class which implements the EmailValidation\EmailDataProviderInterface.
Example Code:
<?php
declare(strict_types=1);
namespace EmailValidation;
class CustomEmailDataProvider implements EmailDataProviderInterface
{
/**
* {@inheritdoc}
*/
public function getEmailProviders(): array
{
return ['custom.com'];
}
/**
* {@inheritdoc}
*/
public function getTopLevelDomains(): array
{
return ['custom'];
}
/**
* {@inheritdoc}
*/
public function getDisposableEmailProviders(): array
{
return ['custom.com', 'another.com'];
}
/**
* {@inheritdoc}
*/
public function getRoleEmailPrefixes(): array
{
return ['custom'];
}
}
No, none of these tests are 100% accurate. As with any email validation there will always be false positives & negatives. The only way to guarantee an email is valid is to send an email and solicit a response. However, this library is still useful for detecting disposable emails etc., and also acts as a good first line of defence.
Yes, this project relies on this great repository for its list of disposable email providers. To fetch the latest list from that repo you can run
./scripts/update-dispsable-email-providers.php
from the command line. This will fetch the data and save it to ./src/data/disposable-email-providers.php