Integrating ZeroBounce Reliable Email Verification In Laravel

Validating user email addresses is critical to ensure high deliverability and to avoid fake or disposable accounts. In a production app where you are providing service on trail phase with high end limits someone can try to create dummy accounts using disposable mail and explore your services.

With help of email validation and verification service like ZeroBounce, you can prevent fake registrations, Improve email deliverability, Reduce bounce rates and spam complaints, Block disposable or spammy signups and avoid wasting emails on inactive or fake users (important for paid email tools like Mailgun or SendGrid)

In this tutorial, we’ll show you how to integrate ZeroBounce email validation while user registration. Let’s assume you have working Laravel application with authentication functionality. Here we will just explore validation rule for validating user sign up email.

Setting Up ZeroBounce Account

Before diving into Laravel, First we need to sign up with ZeroBounce. You can sign up using your email or login with google.

Once account is created, we need to get API key from ZeroBounce. By default Zerobounce will create and attach API key with your account. We can access that API key by https://www.zerobounce.net/members/API.

ZeroBounce Configuration in Application

Once we have configured ZeroBounce and received API key. Now we can move forward with configuring API key into our application.
First of all we will add key into .env file.

ZEROBOUNCE_API_KEY="**************************"

As common practice for Laravel let’s use this API key into our config file. For this example, we will add this key into config/service.php. Open config file and add below code.

.
.
'zerobounce' => [
    'api_key' => env('ZEROBOUNCE_API_KEY')
],

Creating ZeroBounce Service

Create new service called ZeroBounceService using artisan command.

php artisan make:service ZeroBounceService

Above command will generate service class for Zero bounce. Let’s modify it for adding validation of email using zerobounce API.

<?php

namespace App\Library;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class ZeroBounceService
{
    protected $apiKey;

    public function __construct()
    {
        $this->apiKey = config('services.zerobounce.api_key');
    }

    /**
     * Validate an email address using ZeroBounce API.
     *
     * @param string $email
     * @return array|null
     */
    public function validateEmail(string $email): ?array
    {
        $response = Http::get('https://api.zerobounce.net/v2/validate', [
            'api_key' => $this->apiKey,
            'email'   => $email,
        ]);

        if ($response->successful()) {
            return $response->json();
        }

        return null;
    }
}

Service class will call zerobounce API and pass API key and email and get response from API. Although we can move this logic directly into our validation rule but we are taking class based approach for using same functionality for variety of requirement. The same function can be used to get credit of email, domain rating and many more thing.

Email Validation rule for ZeroBounce

Let’s create custom validation rule for utilize zero bounce service. Open terminal and enter below command to create validation rule.

php artisan make:rule ZeroBounceEmailRule

App\Rules\ZeroBounceEmailRule.php

<?php

namespace App\Rules;

use Closure;
use App\Library\ZeroBounceService;
use Illuminate\Contracts\Validation\ValidationRule;

class ZeroBounceEmailRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $service = new ZeroBounceService();
        $result = $service->validateEmail($value);

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

Validation rule will utilize service class and returns data. Here, we will check data for email address is valid then allowing user to process otherwise it will raise validation error.

Now we can use this rule into any validation. Let’s take regular registration example.

<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Rules\ZeroBounceEmailRule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class RegisterController extends Controller
{
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    public function register(Request $request)
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                'unique:users,email',
                new ZeroBounceEmailRule, // ✅ custom validation
            ],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);

        $user = User::create([
            'name' => $validated['name'],
            'email' => $validated['email'],
            'password' => Hash::make($validated['password']),
        ]);

        Auth::login($user);

        return redirect()->route('dashboard'); // or home route
    }
}

Conclusion

Integrating ZeroBounce into your Laravel registration process allows you to proactively block invalid, fake, disposable, or risky email addresses before they ever enter your database. This not only improves the quality of your user data, but also enhances email deliver ability, protects your system from abuse, and reduces bounce rates. With Laravel’s powerful validation features and ZeroBounce’s reliable API, adding real-time email verification to your application is both efficient and easy to maintain.