How to Validate Email Addresses using Laravel Mailbox Layer

Image for post
Image for post
How to Validate Email Addresses using Laravel Mailbox Layer

Introduction

Image for post
Image for post

Why Not Just Use the Built-in PHP Functions?

Getting an API key

MAILBOX_LAYER_API_KEY=your-api-key-here

Installing the Package

composer require ashallendesign/laravel-mailboxlayer

Validating the Email Address

use App\Jobs\CompleteNewsletterSubscription;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NewsletterSubscriptionController extends Controller
{
/**
* Store a new newsletter subscriber.
*
* @param Request $request
* @return JsonResponse
*/
public function store(Request $request): JsonResponse
{
$request->validate([
'email_address' => 'required|email'
]);

CompleteNewsletterSubscription::dispatch();

return response()->json(['success' => true]);
}
}
use App\Jobs\CompleteNewsletterSubscription;
use AshAllenDesign\MailboxLayer\Facades\MailboxLayer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NewsletterSubscriptionController extends Controller
{
/**
* Store a new newsletter subscriber.
*
* @param Request $request
* @return JsonResponse
*/
public function store(Request $request): JsonResponse
{
$request->validate([
'email_address' => 'required|email'
]);

$result = MailboxLayer::check($request->email_address);

if (! $result->smtpCheck) {
abort(422, 'The email address is not valid.');
}

CompleteNewsletterSubscription::dispatch();

return response()->json(['success' => true]);
}
}

Denying Disposable Addresses

use App\Jobs\CompleteNewsletterSubscription;
use AshAllenDesign\MailboxLayer\Facades\MailboxLayer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NewsletterSubscriptionController extends Controller
{
/**
* Store a new newsletter subscriber.
*
* @param Request $request
* @return JsonResponse
*/
public function store(Request $request): JsonResponse
{
$request->validate([
'email_address' => 'required|email'
]);

$result = MailboxLayer::check($request->email_address);

if (! $result->mxFound) {
abort(422, 'The email address is not valid.');
}

if ($result->disposable) {
abort(422, 'The email address is disposable.');
}

CompleteNewsletterSubscription::dispatch();

return response()->json(['success' => true]);
}
}

Adding Caching for Performance Improvements

use App\Jobs\CompleteNewsletterSubscription;
use AshAllenDesign\MailboxLayer\Facades\MailboxLayer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NewsletterSubscriptionController extends Controller
{
/**
* Store a new newsletter subscriber.
*
* @param Request $request
* @return JsonResponse
*/
public function store(Request $request): JsonResponse
{
$request->validate([
'email_address' => 'required|email'
]);

$result = MailboxLayer::shouldCache()->check($request->email_address);

if (! $result->mxFound) {
abort(422, 'The email address is not valid.');
}

if ($result->disposable) {
abort(422, 'The email address is disposable.');
}

CompleteNewsletterSubscription::dispatch();

return response()->json(['success' => true]);
}
}

What Next?

Check It out on GitHub

Written by

I’m a Laravel web developer based in the UK. I specialise in building websites and systems for small businesses to help them grow and increase their sales.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store