zytzagoo / smtp-validate-email

A PHP library for performing email addresses validation via SMTP
GNU General Public License v3.0
437 stars 155 forks source link

Return Invalid Emails with Log Data? #54

Closed rdundon closed 4 years ago

rdundon commented 4 years ago

Hi There!

This is a cool script. I am making a similar email validator that currently just does basic email validation and DNS MX record checking, but not as involved.

I noticed that the results return from validate() show an associative array of emails, with the email addresses as the key and a boolean (true/false) value for whether or not the email was determined to be valid.

Using getLog() for the log data provides useful data. However, there doesn't seem to be a way to easily return the specific error for each email address or domain, which would be very useful for providing a list of invalid emails to clients, etc.

Here is a part of the script we are working on, with a basic example:

/**
 * @param string $email_address Email address to verify
 * @return mixed True on success, throws an exception with reason upon failure
 */
public function verifyEmail($email_address) {
    if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
        throw new \Exception('Email not vaild format');

      /** Etc, you get the idea **/

}

Is there a way to provide the specific errors for the emails/domains already? I dug in the code a bit, but did not see an apparent way.

zytzagoo commented 4 years ago

Yeah, there is no such thing there currently.

Building something to that effect, while covering all possible options/cases could probably be done (with a new option and a new getter for data/log, in order to preserve existing behaviour for existing consumers), but I don't currently have the time (or the need) to do it.

Perhaps a simple approach could work and just "key" debug messages with the "current" address being handled... Or something like opening a new "log level" for each address being handled, but, yeah, it would need more exploration...

rdundon commented 4 years ago

Makes sense. If I have time I could make a PR for this, but no promises :)

rdundon commented 4 years ago

Update: I don't think this is needed by a feature request per se. I was able to add this project as a library and keep things clean :)

For reference to anyone else with the same use case, here is the updated function:

/**
 * @param string $email_address Email address to verify
 * @return mixed True on success, throws an exception with reason upon failure
 */
public function verifyEmail($email_address, $sending_email) {
    if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
        throw new \Exception('Email not vaild format');
    }

    if (!checkdnsrr($this->getEmailDomain($email_address), 'MX')) {
        throw new \Exception('DNS record not valid');
    }

    // Use SMTPValidateEmail\Validator for now, at least
    $smtp_validator = new \SMTPValidateEmail\Validator($email_address, $sending_email);
    $result = $smtp_validator->validate($email_address)[$email_address];
    if (!$result) {
        throw new \Exception("Failed SMTP Validation. Logs are " . implode("\r\n",$smtp_validator->getLog()));
    }
}`

These exceptions are then caught in the calling function, and added in CSV log for debugging / listing why emails failed.