egulias / EmailValidator

PHP Email address validator
MIT License
11.44k stars 208 forks source link

Validation passes for misspelt email addresses. #379

Closed sammyskills closed 1 year ago

sammyskills commented 1 year ago

Hi,

Thanks for the time you took to create this library. I just found out about it and I'm trying to implement it in a project. The issue I'm having is that some users tend to misspell their email address during registration. For example,

email@gmaii.co
email@gmaii.com
email@gmall.con

These are obvious (and human) errors, that makes sending of an activation email to fail, but when I tried to validate for cases like this, the validation passes. Here's my code:

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\Extra\SpoofCheckValidation;
use Egulias\EmailValidator\Validation\DNSCheckValidation;
use Egulias\EmailValidator\Validation\MessageIDValidation;
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
use Egulias\EmailValidator\Validation\RFCValidation;

$validator = new EmailValidator();
$multipleValidator = new MultipleValidationWithAnd([
    new RFCValidation(),
    new DNSCheckValidation(),
    new SpoofCheckValidation(),
    new MessageIDValidation(),
    new NoRFCWarningsValidation()
]);
var_dump($validator->isValid('example@gmaii.com', $multipleValidator)); // bool(true)
var_dump($validator->isValid('example@gmaii.co', $multipleValidator)); // bool(true)
var_dump($validator->isValid('example@gmall.com', $multipleValidator)); // bool(true)

Is there a better way or workaround for this type of issue?

egulias commented 1 year ago

Hi @sammyskills . While potentially typos, from the perspective of the RFCs those are valid emails. If you want to test against typos for known email services as part of the validation of the format, you can extend using the EmailValidation interface (check the readme) and implement there your own logic, then using that class as one of the validators as you are doing with the other in the example.

sammyskills commented 1 year ago

Thanks for the reply @egulias. But, I'm a bit confused, is the DNSCheckValidation not supposed to catch/check such errors? Like doing something like this:

$validator = new EmailValidator();
var_dump($validator->isValid('someemail@gmaii.com', new DNSCheckValidation())); // returns bool(true)

One would expect this to return false, or am I missing something?

egulias commented 1 year ago

Hi @sammyskills . Sadly, yes and no :). As you might find on other issues for the validator, the DNS check gives some false positives given how DNS report the existence of an email sever behind a given domain.

Have you tried to see what kind of records those domains are returning? For instance: https://who.is/dns/gmaii.com As you can see it does has an MX record, which will make the validator return true.

sammyskills commented 1 year ago

Oh, that explains it all.

Thanks @egulias.