Categories
Tutorial

Blocking Disposable and Nonexistent User Emails

With disposable email websites readily available it’s important to add an extra verification layer to your sign ups, and email opt-ins! Plus, who needs all those extra rows clogging up your database. In this post I’ll show you how to detect disposable emails, and nonexistent domains. Be sure to check out Cake’s email-queue plugin, along with some of my other favorites.

Validating… with pizza?

Luckily there is already a free API that allows you to check an inputted email, or domain to see if it’s legitimate: Validator.pizza! First I’ll go over the API itself, then I’ll show you how to do it in CakePHP

Domains

In order to validate a domain all you need to do is send a GET request

GET https://www.validator.pizza/domain/example.com

This will give a JSON response that will indicate whether or not it is a valid domain

Valid:

{
    "status": 200,
    "domain": "example.com",
    "mx": false,
    "disposable": false,
    "did_you_mean": false,
    "remaining_requests": 119
}

Invalid:

{
    "status": 400,
    "error": "The domain is invalid."
}

Email

Checking whether an email belongs to a valid domain is just as easy. Just replace ‘domain’ with ’email’, give it an email, and see what happens!

GET https://www.validator.pizza/email/[email protected]

Valid:

{
    "status": 200,
    "email": "[email protected]",
    "domain": "example.com",
    "mx": false,
    "disposable": false,
    "alias": false,
    "did_you_mean": false,
    "remaining_requests": 119
}

Invalid:

{
    "status": 400,
    "error": "The email address is invalid."
}

Cake and Pizza 😋

We can use CakePHP’s built-in HttpClient to send a GET request and get a response object.

First include HttpClient in your controller.

use Cake\\Http\\Client;

Then create a new client object, send a request, and get your response!

$http = new Client();
$response = $http->get('https://www.validator.pizza/email/[email protected]');
$response = $response->getJson();

This will store the following in $response:

[
	'status' => (int) 200,
	'email' => '[email protected]',
	'domain' => 'example.com',
	'mx' => true,
	'disposable' => false,
	'alias' => false,
	'did_you_mean' => null,
	'remaining_requests' => (int) 119
]

It’s that simple!

Now that you’ve seen how to integrate validator.pizza‘s email validation will you use it? Let me know in the comments below if I missed anything.

Don’t forget to subscribe to the blog for more helpful posts like this!