Skip to content

E-commerce & payments

Validate customer emails in Magento and Adobe Commerce

A small Magento 2 module with a plugin on AccountManagementInterface::createAccount that refuses undeliverable and disposable emails with a translatable error.

Updated 2026-11-26

This recipe adds a small Magento 2 module that checks a new customer's email before the account is created. An address that cannot receive mail, or a disposable inbox, gets a clear error on the registration form, and an API problem never blocks a signup. It works the same on Magento Open Source and Adobe Commerce 2.4. There is no native Qelka extension for Magento, so the module is yours to own.

What you need

  • Magento Open Source or Adobe Commerce 2.4 on PHP 8.1 or later, with shell access to run bin/magento.
  • A Qelka API key. A sk_test_ key returns simulated results and never spends credits: see Test mode. Restrict it to email: see Restricted keys.
  • A vendor name for the module. This guide uses Acme_SignupValidation; replace Acme with yours.

Steps

1. Store the key

Add a section to app/etc/env.php, at the top level of the returned array:

php
'signup_validation' => [
    'api_key' => 'sk_live_your_key',
],

Magento's DeploymentConfig reads it as signup_validation/api_key. Magento 2.4.4 and later also read the environment variable MAGENTO_DC_SIGNUP_VALIDATION__API_KEY, which overrides the file: that suits container and cloud deployments where secrets come from the environment, and then you can leave env.php alone.

2. Register the module

Create app/code/Acme/SignupValidation/registration.php:

php
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Acme_SignupValidation', __DIR__);

And app/code/Acme/SignupValidation/etc/module.xml:

xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Acme_SignupValidation">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

3. The client

Create app/code/Acme/SignupValidation/Model/EmailChecker.php. It returns the API's answer, or null when the check could not complete:

php
<?php

declare(strict_types=1);

namespace Acme\SignupValidation\Model;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\HTTP\Client\CurlFactory;
use Psr\Log\LoggerInterface;

class EmailChecker
{
    public function __construct(
        private readonly CurlFactory $curlFactory,
        private readonly DeploymentConfig $deploymentConfig,
        private readonly LoggerInterface $logger
    ) {
    }

    public function check(string $email): ?array
    {
        $key = (string) $this->deploymentConfig->get('signup_validation/api_key', '');
        if ($key === '' || $email === '') {
            return null;
        }

        for ($attempt = 1; $attempt <= 2; $attempt++) {
            $curl = $this->curlFactory->create();
            $curl->setTimeout(5);
            $curl->addHeader('Authorization', 'Bearer ' . $key);
            $curl->addHeader('Content-Type', 'application/json');

            try {
                $curl->post('https://api.qelka.com/v1/email', (string) json_encode(['email' => $email, 'mode' => 'fast']));
            } catch (\Exception $exception) {
                $this->logger->warning('Email validation skipped: unreachable', ['message' => $exception->getMessage()]);
                return null;
            }

            $status = $curl->getStatus();
            $data = json_decode($curl->getBody(), true);
            $headers = array_change_key_case($curl->getHeaders(), CASE_LOWER);

            if ($status === 200 && is_array($data)) {
                return $data;
            }

            $retryAfter = (int) ($headers['retry-after'] ?? 0);
            if ($attempt === 1 && in_array($status, [429, 500, 502, 503], true) && $retryAfter <= 2) {
                sleep(max($retryAfter, 1));
                continue;
            }

            $this->logger->warning('Email validation skipped', [
                'type' => $data['error']['type'] ?? 'unknown',
                'status' => $status,
                'request_id' => $headers['x-request-id'] ?? null,
            ]);
            return null;
        }

        return null;
    }
}

CurlFactory is generated by Magento, and a new client per call keeps headers from leaking between requests. Magento\Framework\HTTP\Client\Curl::post sends a string body as is, which is how the JSON goes out, and setTimeout caps the whole request in seconds. It throws on a network error, which the code turns into null.

4. The plugin

Create app/code/Acme/SignupValidation/Plugin/ValidateEmailBeforeAccountCreation.php:

php
<?php

declare(strict_types=1);

namespace Acme\SignupValidation\Plugin;

use Acme\SignupValidation\Model\EmailChecker;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;

class ValidateEmailBeforeAccountCreation
{
    public function __construct(private readonly EmailChecker $emailChecker)
    {
    }

    public function beforeCreateAccount(
        AccountManagementInterface $subject,
        CustomerInterface $customer,
        $password = null,
        $redirectUrl = ''
    ): ?array {
        $result = $this->emailChecker->check((string) $customer->getEmail());
        if ($result === null) {
            return null;
        }

        if ($result['status'] === 'undeliverable') {
            throw new LocalizedException(
                $result['did_you_mean']
                    ? __('This email address cannot receive mail. Did you mean %1?', $result['did_you_mean'])
                    : __('This email address cannot receive mail.')
            );
        }

        if ($result['status'] === 'risky' && $result['reason'] === 'disposable') {
            throw new LocalizedException(__('Please use a permanent email address, not a disposable one.'));
        }

        return null;
    }
}

A before plugin that returns null leaves the arguments unchanged. Throwing stops createAccount before anything is saved. The storefront's Customer\Controller\Account\CreatePost catches a LocalizedException, shows its message and sends the shopper back to the form with what they typed.

Declare the plugin in app/code/Acme/SignupValidation/etc/frontend/di.xml:

xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Api\AccountManagementInterface">
        <plugin name="acme_signup_validation_email" type="Acme\SignupValidation\Plugin\ValidateEmailBeforeAccountCreation"/>
    </type>
</config>

The frontend area keeps the check on the storefront, so staff creating customers in the admin and imports are not slowed down. Copy the file to etc/graphql/ and etc/webapi_rest/ for headless storefronts.

5. Translate the messages

Because the messages go through __(), Magento translates them like its own. Add app/code/Acme/SignupValidation/i18n/fr_FR.csv, and one file per store locale:

csv
"This email address cannot receive mail.","Cette adresse e-mail ne peut pas recevoir de messages."
"This email address cannot receive mail. Did you mean %1?","Cette adresse e-mail ne peut pas recevoir de messages. Vouliez-vous dire %1 ?"
"Please use a permanent email address, not a disposable one.","Merci d'utiliser une adresse e-mail permanente, pas une adresse jetable."

6. Enable and test

bash
bin/magento module:enable Acme_SignupValidation
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:flush

setup:di:compile is needed in production mode. With a sk_test_ key, register with nomx@example.com to see the undeliverable error and disposable@example.com to see the disposable one; any other well-formed address goes through. See Test mode.

What the module does with each result

Result Registration
undeliverable Refused, with did_you_mean when there is one
risky, reason disposable Refused
risky, catch-all or full mailbox Accepted
unknown, including smtp_not_checked Accepted
API unreachable, slow, out of credits Accepted, logged in var/log/system.log

mode: 'fast' runs the syntax, DNS, disposable and role checks without contacting the shopper's mail server, so the registration page does not wait on a server you do not control. It never answers deliverable, only unknown with smtp_not_checked when the address looks fine. For a mailbox check, drop mode and raise the timeout to 15 seconds. See the email reference.

To manage the key in the admin instead of env.php, add a system.xml field with the backend model Magento\Config\Model\Config\Backend\Encrypted, read it with Magento\Framework\App\Config\ScopeConfigInterface and decrypt it with Magento\Framework\Encryption\EncryptorInterface.

Handle errors and rate limits

Nothing is billed on errors. 401 invalid_api_key, 403 forbidden and 402 insufficient_credits let the signup through and write a warning with the error type and request id to the log, so monitor it or checks stop silently. A 429 or 5xx is retried once when Retry-After is 2 seconds or less. A key allows 200 requests per 10 seconds: keep your CAPTCHA on the registration form, or a bot can use your credits. Each email check costs 1 credit. See Errors and Rate limits.

Frequently asked questions

Plugin or observer?
A plugin. Magento's customer events, such as customer_register_success, fire after the account is saved, so an observer cannot stop it cleanly. A before plugin on AccountManagementInterface::createAccount runs first, and a LocalizedException thrown there aborts the creation and reaches the shopper as an error message.
Does it cover the GraphQL and REST APIs of a headless store?
Only if you declare the plugin in those areas too. The module below declares it in etc/frontend/di.xml, which covers the storefront registration form. Copy the same di.xml to etc/graphql and etc/webapi_rest to cover a PWA or another headless front, or move it to etc/di.xml to cover every area, including customers created in the admin.
Where should the API key live?
In app/etc/env.php, which Magento keeps out of version control and outside the web root, or in the environment variable that Magento 2.4 maps onto it. Never in a module file, a theme or JavaScript. If your team prefers the admin, store it in an encrypted configuration field instead.

More in E-commerce & payments

All integrations

Put this check in your product

Get an API key in under a minute. 100 free credits every month, no card required.

  • 100 free credits a month
  • No card
  • Test keys spend nothing
  • Cancel any time