How to Block Spam E-mail from registering on Magento2 site

Today the internet is surging with spammers who are dying to get their hands on your servers. Spammers register an account on your site to brake into your system as it is a relatively easy way for amateur. It is one of the key parts of their spamming strategy. We cannot stress enough the importance of identifying spam domains and blocking them in advance because whether human or a bot machines, spammers would normally be using an email spam domain. You need to integrate your web store with additional anti-spam protection making definite email domains and zones blocked. If you allow your customers to use guest checkout, don’t forget it is hassle-free for spammers or hackers.

With Restrict Domain Registration feature you can block users from registering for accounts using email addresses from certain domains. This feature is useful for blocking spam and bots from logging into your website and it will effectively increase the security of the store. You can easily put the domains to block and set the error message to be displayed when a user attempts to register with an email domain on your block list. Full instructions are as follows –

The coding starts here…

Create a new module with a name EmailCheck in the folder Ecomsolver

Step – 1  Write the following code at admin panel. Path of the file will be – Ecomsolver >EmailCheck > etc > Adminhtml > System


  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
  3. <system>
  4. <tab id="ecomsolver" translate="label" sortOrder="999">
  5. <label>Ecomsolver</label>
  6. </tab>
  7. <section id="emailblock" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
  8. <class>separator-top</class>
  9. <label>Email Check</label>
  10. <tab>ecomsolver</tab>
  11. <resource>PixieMedia_General::general_config</resource>
  12. <group id="domains" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
  13. <label>Domain Names</label>
  14. <field id="domains" translate="label" type="textarea" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
  15. <label>Domain names to block</label>
  16. <comment>Comma separated values eg google.co.uk,mail.ru,some.com</comment>
  17. </field>
  18. <field id="message" translate="label" type="textarea" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
  19. <label>Message to display</label>
  20. <comment>The error message to show users who try to register with one of the above domain names</comment>
  21. </field>
  22. </group>
  23. </section>
  24. </system>
  25. </config>

Step – 2 Write the following code in a file. Path of the file will be – Ecomsolver >EmailCheck > etc > Frontend > di


  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  4. <type name="Magento\Customer\Controller\Account\CreatePost">
  5. <plugin name="restrictCustomerEmail"
  6. type="Ecomsolver\Emailcheck\Model\Plugin\Controller\Account\RestrictCustomerEmail"/>
  7. </type>
  8. </config>

Step – 3 Write the following code in XML file with name Config. Path of the file will be – Ecomsolver >EmailCheck > etc > Config


  1. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
  3. <default>
  4. <emailblock>
  5. <domains>
  6. <domains>163.com,mail.ru</domains>
  7. </domains>
  8. <message>
  9. <domains>We do not allow registration from your email domain</domains>
  10. </message>
  11. </emailblock>
  12. </default>
  13. </config>

Step – 4 Write the following code in XML file with name Module. Path of the file will be – Ecomsolver >EmailCheck > etc > Module

  1. <config xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
  2. <module name="Ecomsolver_Emailcheck" setup_version="1.0.0">
  3. </module>
  4. </config>

Step – 5  Create the folder name Model in EmailCheck. Then create subfolder Plugin > Controller > Account. Write the following code in php file with name RestrictCustomerEmail. Path of the php file will be – Ecomsolver >EmailCheck > Plugin > Controller > Account > RestrictCustomerEmail


  1. /* Ecomsolver @@@@@@ ecomsolver@gmail.com */
  2. namespace Ecomsolver\Emailcheck\Model\Plugin\Controller\Account;
  3. use Magento\Framework\Controller\Result\RedirectFactory;
  4. use Magento\Framework\UrlFactory;
  5. use Magento\Framework\Message\ManagerInterface;
  6. use Magento\Framework\App\Config\ScopeConfigInterface;
  7. class RestrictCustomerEmail
  8. {
  9. /** @var \Magento\Framework\UrlInterface */
  10. protected $urlModel;
  11. /**
  12. * @var \Magento\Framework\Controller\Result\RedirectFactory
  13. */
  14. protected $resultRedirectFactory;
  15. /**
  16. * @var \Magento\Framework\Message\ManagerInterface
  17. */
  18. protected $messageManager;
  19. /**
  20. * RestrictCustomerEmail constructor.
  21. * @param UrlFactory $urlFactory
  22. * @param RedirectFactory $redirectFactory
  23. * @param ManagerInterface $messageManager
  24. */
  25. public function __construct(
  26. UrlFactory $urlFactory,
  27. RedirectFactory $redirectFactory,
  28. ManagerInterface $messageManager,
  29. ScopeConfigInterface $scopeConfig
  30. )
  31. {
  32. $this->urlModel = $urlFactory->create();
  33. $this->resultRedirectFactory = $redirectFactory;
  34. $this->messageManager = $messageManager;
  35. $this->scopeConfig = $scopeConfig;
  36. }
  37. /**
  38. * @param \Magento\Customer\Controller\Account\CreatePost $subject
  39. * @param \Closure $proceed
  40. * @return mixed
  41. * @throws \Magento\Framework\Exception\LocalizedException
  42. */
  43. public function aroundExecute(
  44. \Magento\Customer\Controller\Account\CreatePost $subject,
  45. \Closure $proceed
  46. )
  47. {
  48. /** @var \Magento\Framework\App\RequestInterface $request */
  49. $email = $subject->getRequest()->getParam('email');
  50. list($nick, $domain) = explode('@', $email, 2);
  51. $domains = $this->scopeConfig->getValue('emailblock/domains/domains', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
  52. if(!$domains) {
  53. return $proceed;
  54. }
  55. $domainArray = array_map('trim', explode(',', $domains));
  56. if(count($domainArray) < 1) {
  57. return $proceed;
  58. }
  59. if (in_array($domain, $domainArray, true)) {
  60. $message = $this->scopeConfig->getValue('emailblock/domains/message', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
  61. if(!$message) { $message = __('We do not allow registration from your email domain'); }
  62. $this->messageManager->addErrorMessage($message);
  63. $defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
  64. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  65. $resultRedirect = $this->resultRedirectFactory->create();
  66. return $resultRedirect->setUrl($defaultUrl);
  67. }
  68. return $proceed();
  69. }
  70. }

Using this module you can block registration from certain domains and make your security level higher. Spammers and fraudulent orders can cause sturdy damage to your store; hence it will result in bad reputation. If you have any queries or feedback, then feel free to drop us a line.  Ecomsolver is a company promoted by a group of highly experienced professionals. Specialize in providing top-notch web solutions through innovation and use of latest technology.

Comments