daveearley / daves-email-validation-tool
An easy to use, accurate-ish & extensible email validation library for PHP 7+
Installs: 245 467
Dependents: 5
Suggesters: 0
Security: 0
Stars: 277
Watchers: 19
Forks: 44
Open Issues: 6
Requires
- php: >=7.4
- ext-json: *
Requires (Dev)
- mockery/mockery: ^1.3
- phpunit/phpunit: ^9.3
- dev-master
- 1.2.0
- v1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-update-providers/top-level-domains
- dev-bump-mockery-phpunit-versions
- dev-ISSUE-22-DNS-check-update
- dev-more-php7.4-updates
- dev-php7.4
- dev-daveearley-patch-1
- dev-fix-yahoo.no-false-negative
- dev-analysis-XpgLmo
- dev-develop
This package is auto-updated.
Last update: 2024-11-13 18:19:07 UTC
README
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:
^ Data used for these checks can be found here
Installation
composer require daveearley/daves-email-validation-tool
Usage
Quick Start
// Include the composer autoloader require __DIR__ . '/vendor/autoload.php'; $validator = EmailValidation\EmailValidatorFactory::create('dave@gmoil.con'); $jsonResult = $validator->getValidationResults()->asJson(); $arrayResult = $validator->getValidationResults()->asArray(); echo $jsonResult;
Expected output:
{ "valid_format": true, "valid_mx_records": false, "possible_email_correction": "dave@gmail.com", "free_email_provider": false, "disposable_email_provider": false, "role_or_business_email": false, "valid_host": false }
Adding Custom Validations
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.
Example code
// 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('dave@gmail.com'); $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 }
Running in Docker
docker-compose up -d
You can then validate an email by navigating to http://localhost:8880?email=email.to.validate@example.com. The result will be JSON string as per above.
Adding a custom data source
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 { public function getEmailProviders(): array { return ['custom.com']; } public function getTopLevelDomains(): array { return ['custom']; } public function getDisposableEmailProviders(): array { return ['custom.com', 'another.com']; } public function getRoleEmailPrefixes(): array { return ['custom']; } }
FAQ
Is this validation accurate?
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.
Can I manually update the disposable email provider data?
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