Anyone know how to block customer registration with specific email extension? I am getting so many automated user registrations with [email protected] and [email protected] how to block this type email registrations?

2 Answers 2


In your case, I think we can try with Plugin. The Magento\Customer\Controller\Account\CreatePost controller will execute the customer registration request. So, we can build the logic with this class.


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    <type name="Magento\Customer\Controller\Account\CreatePost">
        <plugin name="restrictCustomerEmail"



namespace Company\Module\Model\Plugin\Controller\Account;

use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\UrlFactory;
use Magento\Framework\Message\ManagerInterface;

class RestrictCustomerEmail

    /** @var \Magento\Framework\UrlInterface */
    protected $urlModel;

     * @var \Magento\Framework\Controller\Result\RedirectFactory
    protected $resultRedirectFactory;

     * @var \Magento\Framework\Message\ManagerInterface
    protected $messageManager;

     * RestrictCustomerEmail constructor.
     * @param UrlFactory $urlFactory
     * @param RedirectFactory $redirectFactory
     * @param ManagerInterface $messageManager
    public function __construct(
        UrlFactory $urlFactory,
        RedirectFactory $redirectFactory,
        ManagerInterface $messageManager

        $this->urlModel = $urlFactory->create();
        $this->resultRedirectFactory = $redirectFactory;
        $this->messageManager = $messageManager;

     * @param \Magento\Customer\Controller\Account\CreatePost $subject
     * @param \Closure $proceed
     * @return mixed
     * @throws \Magento\Framework\Exception\LocalizedException
    public function aroundExecute(
        \Magento\Customer\Controller\Account\CreatePost $subject,
        \Closure $proceed
        /** @var \Magento\Framework\App\RequestInterface $request */
        $email = $subject->getRequest()->getParam('email');
        list($nick, $domain) = explode('@', $email, 2);
        if (in_array($domain, ['163.com', 'mail.ru'], true)) {

                'Registration is disabled for you domain'
            $defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();

            return $resultRedirect->setUrl($defaultUrl);

        return $proceed();

NOTE: This way can prevent customer who registers via Front page. It's better if we check before saving customer entity.


You should create plugin on \Magento\Customer\Api\CustomerRepositoryInterface::save method and verify email of customer.

For example:

namespace Vendor\Module\Plugin; 
class CustomerValidation 
     * Before customer save.
     * @param CustomerRepositoryInterface $customerRepository
     * @param CustomerInterface $customer
     * @param null $passwordHash
     * @return array
    public function beforeSave(
        \Magento\Customer\Api\Data\CustomerInterface $customer,
        $passwordHash = null
    ) {
       list($nick, $domain) = explode('@', $customer->getEmail(), 2);

       if (in_array($domain, ['163.com', 'mail.ru'], true)) {
           throw new \Magento\Framework\Exception\LocalizedException(
                    'Registration is disabled for you domain'

        return [$customer, $passwordHash];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.