Skip to content

This repository provides a PHP function to validate the domain of an email address using DNS over HTTPS (DoH) service providers.

Notifications You must be signed in to change notification settings

cdemers/PHPValidateEmailDomain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Email Domain Validation Function

This repository provides a PHP function to validate the domain of an email address using DNS over HTTPS (DoH) service providers.

The function, validateEmailDomainWithDoH(), validates an email address by sending a DNS query to one or more DoH server (with some reputable defaults included) and checking if the domain of the email has an MX record, and if this MX record is flagged as malware related or not.

Table of Contents

Getting Started

The function was tested with PHP 8, but it should work with PHP 7. It requires the cURL and JSON PHP extensions to be installed and activated. It also uses the PSR-3 LoggerInterface, and so a PSR-3 compliant logger (like Monolog) should be given to the function for logging.

It's not Composer ready because it's (almost) a single function, but you can copy the function in your project and use it directly. Also, sometimes I'm lazy.

Usage

Here is a simple usage example:

$log = new Psr\Log\Logger();

try {
    if (validateEmailDomainWithDoH('user.name@example.com', $log)) {
        echo "Valid domain\n";
    } else {
        echo "Invalid domain\n";
    }
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

The function validateEmailDomainWithDoH() takes a string as it's first argument, which should be the email address that you want to validate, and a PSR LoggerInterface compliant logger as it's second parameter. The function returns true if the domain of the email is valid, and false if it is not valid or if it's considered dangerous by the DoH server. The function throws an exception if an error occurs while executing the validation.

How it Works

The function works by making a DNS query for the MX record of the email's domain using a DoH service.

The MX (Mail Exchanger) record is a type of resource record in the Domain Name System that specifies a mail server responsible for accepting email messages on behalf of a domain.

If one DoH service is not available, it cycles through the list of DoH services until it finds one that is available. If none of the DoH services are available, it throws an exception.

Note that this function does not check if the email address is actually in use. It only checks if the domain of the email address has an MX record.

Precautions

This function is depending on third-party DoH servers. Because of this, its functionality could be affected by network issues, downtime, or changes in these services. Always make sure to handle the possibility that an exception might be thrown.

References

Contributing

Feel free to submit pull requests to enhance the functionality of the validateEmailDomainWithDoH() function.

License

MIT License

Copyright (c) 2023 Charle Demers

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

This repository provides a PHP function to validate the domain of an email address using DNS over HTTPS (DoH) service providers.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages