Mastering Laravel: Temporary and Disposable Email Validation in Laravel

Validating user input is a critical part of any web application. One common aspect of this validation is ensuring that users enter a valid email address. However, it can be difficult to validate email addresses that are temporary or disposable, such as those from services like Mailinator or Guerrilla Mail. Fortunately, Laravel makes it easy to implement temporary email validation with custom validation rules.

Let’s call our rule TempEmail. To get started, we can use the php artisan make:rule TempEmail command to create a new validation rule for temporary emails. This will create a new class in the App/Rules directory that we can use to implement our validation logic.

To validate temporary email, add the following code to the TempEmail class. This code checks a GitHub gist for blacklisted temporary email domains and caches the results for 10 minutes. Any updates to the gist will also be reflected in the cached results. The code then implements a basic logic to compare the email domain against the blacklist.

<?php

namespace App\Rules;

use Cache;
use Illuminate\Contracts\Validation\Rule;

class TempEmail implements Rule
{
protected $blacklistedDomains;

public function __construct()
{
$this->blacklistedDomains = Cache::remember('TempEmailBlackList', 60 * 10, function () {
$data = @file_get_contents('https://gist.githubusercontent.com/saaiful/dd2b4b34a02171d7f9f0b979afe48f65/raw/2ad5590be72b69a51326b3e9d229f615e866f2e5/blocklist.txt');
if ($data) {
return array_filter(array_map('trim', explode("\n", $data)));
}
return [];
});
}

public function passes($attribute, $value)
{
$emailDomain = substr(strrchr($value, "@"), 1);
return !in_array($emailDomain, $this->blacklistedDomains);
}

public function message()
{
return 'This email not allowed.';
}
}

Next, we can add our new validation rule to the list of rules that Laravel uses when validating user input. We can do this by opening the AppServiceProvider and adding the following code to the boot method:

use App\Rules\TempEmail;
use Illuminate\Support\Facades\Validator;

...

Validator::extend('temp_email', function ($attribute, $value, $parameters, $validator) {
return (new TempEmail())->passes($attribute, $value);
}, (new TempEmail())->message());

This code registers a new validation rule called temp_email that uses our TempEmail rule class to validate email addresses. Whenever this rule is used in a validation, Laravel will call the passes method of the TempEmail class to determine whether the email address is valid.

Now that we have registered our new validation rule, we can use it in any Laravel validation just like any other rule. For example, we could use it to validate a user’s email address like this:

$validator = Validator::make($request->all(), [
'email' => 'required|email|temp_email',
]);

// or

$request->validate([
'email' => 'required|email|temp_email',
]);

This will ensure that the email field is required, contains a valid email address, and is not a temporary email address.

With these simple steps, we can easily implement temporary email validation in our Laravel applications. By creating a custom validation rule and registering it with Laravel, we can ensure that our users are entering valid email addresses and prevent the use of temporary or disposable email addresses.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.