Git Product home page Git Product logo

email-validation-tool's Introduction

PHP Email Validation Tool

codecov Build Status Code Climate

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

Installation

composer require daveearley/daves-email-validation-tool

Usage

Quick Start

// 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
}

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('[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
}

Running in Docker

docker-compose up -d 

You can then validate an email by navigating to http://localhost:[email protected]. 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

email-validation-tool's People

Contributors

antonkomarev avatar colinodell avatar daveearley avatar khanhicetea avatar misagh avatar mwebber92 avatar notfloran avatar pimpeters avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

email-validation-tool's Issues

Forgot to install packages via composer in Docker image

Error log :

Warning: require(/web/vendor/autoload.php): failed to open stream: No such file or directory in /web/index.php on line 5

Fatal error: require(): Failed opening required '/web/vendor/autoload.php' (include_path='.:/usr/local/lib/php') in /web/index.php on line 5

Context : user clone source and forgot running composer install

Case sensitive free emails

Hi Dave,

I noticed some free emails getting through our filter and could not figure out how it was happening until I started logging the data of the requests.

Turns out [email protected] == free but [email protected] == not free. I can't see this on the docs and wasn't sure if this was deliberate or not?

Screenshot:
image

Happy to provide a PR if this is incidental, just wanted to confirm first.

Custom data provider class

Hi,

Need some guidance because I can't seem to understand the method that takes care of the Custom Email Provider.
I created a new Email Provider that extends EmailValidation\EmailDataProviderInterface, but how do I apply it to the \EmailValidation class?

My code:

$provider = new CustomEmailDataProvider; // my Custom Provider
$validator = \EmailValidation\EmailValidatorFactory::create('[email protected]);
$validator->registerValidator(new CustomValidator()); // my Custom Validator, that works with no issues
$result = $validator->getValidationResults()->asArray();

How do I implement the custom $provider into the \EmailValidation\EmailValidatorFactory ?

Thanks in advance.

Will gmail and/or other mailhosts block my ip?

Hello -

Should I consider the need for a proxy or rate-limiting? Do you know if anyone has been blacklisted for using this package in high-volume situations?

My initial test is ~30,000 emails. But I have a list of 450,000 that I would like to clean up

Thanks again! Beautiful package, and super easy to use.

bug with top leveldomains script

Hi All,
I experience a bug when i try to manually run the script for top level domains with latest https://data.iana.org/TLD/tlds-alpha-by-domain.txt

It seems that this file doesn't work anymore with explode(PHP_EOL, $topLevelDomains) and returns plain string instead of array...
and later
if (!is_array($topLevelDomains)) { die('Unable to parse domains'); } doesn't stop, because result is empty array (from array_shift($topLevelDomains);)

Can i suggest we just replace explode(PHP_EOL, $topLevelDomains) with preg_split('/\r\n|\r|\n/', $topLevelDomains); ?
I have tested and this approach works correctly for mะต.

Thanks

valid_mx_records returns true always

Hi

I installed Email-Validation-Tool as a Docker container.

I tried with many non-existing domains but valid_mx_records response is always "true" all domains.

host -t mx nothinghere123.com

Host nothinghere123.com not found: 3(NXDOMAIN)

when I try with http://myIP:8880/[email protected]
{
"valid_format": true,
"valid_mx_records": true,
"possible_email_correction": "",
"free_email_provider": false,
"disposable_email_provider": false,
"role_or_business_email": false,
"valid_host": false
}

Error during 'composer require'

When running composer require daveearley/daves-email-validation-tool

I get:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for daveearley/daves-email-validation-tool dev-master -> satisfiable by daveearley/daves-email-validation-tool[dev-master].
    - Can only install one of: daveearley/daves-email-validation-tool[v1.1.0, dev-master].
    - Installation request for daveearley/daves-email-validation-tool ^1.1 -> satisfiable by daveearley/daves-email-validation-tool[v1.1.0].

This is on MacOS Catalina, php 7.4.1. Have tried reinstalling, updating composer, php, etc.

international domains

Hi
Can you please confirm if your validator understands internationalized domain names (RFC 5891) or Internationalized local parts (as per SMTPUTF8).

regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.