Intregrate Neverbounce email validation in Laravel

NeverBounce email validation in Laravel is one of the most effective ways to ensure your application collects only valid and deliverable emails. Whether you are sending account confirmations, order updates, or newsletters, validating emails is critical. Invalid or fake addresses can damage your sender reputation, increase bounce rates, and waste valuable resources.

In this guide, we’ll walk through how to integrate NeverBounce email validation in Laravel. We’ll set up an API key, create a custom validation rule, and apply it in a real-world contact form example.

Why Use NeverBounce for Email Validation?

NeverBounce is a reliable email verification service that checks email addresses for validity and deliverability in real time. By integrating NeverBounce email validation in Laravel, you ensure that only working emails enter your system.

Some key benefits of NeverBounce include:

  • Prevent fake sign-ups and protect your platform.
  • Improve email deliverability and inbox placement.
  • Save marketing costs by keeping your mailing list clean.
  • Boost trust with accurate user data.

If you’re exploring more options, we’ve also covered alternatives like ZeroBounce
and Kickbox in separate blog. Feel free to check them out too.

Why Email Validation Is Useful

Email validation is not just a nice-to-have, it’s essential for almost every application:

  • Newsletter Subscriptions: Prevent fake emails from clogging your list.
  • User Registration: Ensure genuine accounts are created.
  • E-commerce Checkout: Deliver invoices and order confirmations successfully.
  • Event Registrations: Avoid fake sign-ups for webinars or workshops.
  • Contact Forms: Capture real leads instead of spam emails.

Setting Up NeverBounce Account and Generate API Key

Before integrating with Laravel, you need to create a NeverBounce account. You can sign up using your email or social login. Once logged in, NeverBounce automatically generates an API key for your account.

You can view or manage your API key anytime from the NeverBounce
. This key is required to connect your Laravel application with NeverBounce.

NeverBounce integration in Laravel application

Setup API Key in Laravel Application

First of all, we will setup API key to our application’s environment file. Open .env file from root directory and add following line:

NEVERBOUNCE_API_KEY=your_api_key_here

Then, you can access it anywhere in your Laravel application using env(‘NEVERBOUNCE_API_KEY’). But for best practice, we will move this key into configuration file for optimization. Let’s add this key into app config file:

.
.
'neverbounce_api_key' => env('NEVERBOUNCE_API_KEY', ''),

Custom Validation Rule for NeverBounce Email Validation

Let’s create a custom validation rule that calls the NeverBounce API to check if an email is valid so it can be reused for another purpose too. Open your terminal and enter below command in it:

php artisan make:rule NeverBounceEmail

In app/Rules/NeverBounceEmail.php:

<?php
namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Http;

class NeverBounceEmail implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $response = Http::get('https://api.neverbounce.com/v4/single/check', [
            'key'   => config('app.neverbounce_api_key'),
            'email' => $value,
        ]);

        $result = $response->json();

        if (!isset($result['result']) || $result['result'] !== 'valid') {
            $fail("The {$attribute} is not a valid email address.");
        }
    }
}

This rule simply checks the given email address is valid or not using NeverBounce. If the email is not marked as valid, Laravel will reject it.

Validating a Contact Form

Now, let’s see implementation of this custom rule in a contact form controller. Here, we will validate contact form for this example:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Rules\NeverBounceEmail;

class ContactController extends Controller
{
    public function submit(Request $request)
    {
        $validated = $request->validate([
            'name'  => ['required', 'string'],
            'email' => ['required', 'email', new NeverBounceEmail],
            'message' => ['required', 'string'],
        ]);

        // Process the contact form (e.g., save or send email)
        return back()->with('success', 'Your message has been sent successfully!');
    }
}

Conclusion

Email validation is a must-have feature for any application that relies on communication or marketing. By integrating NeverBounce email validation in Laravel, you can ensure your email list stays clean, deliverable, and save valuable resources or reduce span delivery.