Skip to content

magenettic/module-email-validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magenettic_EmailValidation

Email validation rules module for Magento 2.4.x

Overview

This module adds custom email validation rules for customer registration, allowing merchants to block or allow specific email domains and addresses.

Features

1. Blocked Email Domains (Blacklist)

Rejects customer registrations with emails from specified domains.

Example: If terminator.com is in the blacklist, any email like john@terminator.com will be rejected.

2. Allowed Email Domains (Whitelist)

Stores a list of allowed email domains.

Example: If company.com is in the whitelist, emails like john@company.com will be allowed.

3. Force Whitelist

When enabled, only emails from domains in the whitelist will be accepted. All other domains will be blocked.

4. Blocked Emails (Blacklist)

Rejects specific email addresses completely.

Example: If spam@example.com is in the list, this exact email will be rejected even if the domain is not blocked.

Configuration

Navigate to: Stores > Settings > Configuration > Customers > Customer Configuration > Email Validation

Setting Description
Enable Email Validation Enable/disable the module
Blocked Email Domains Enter domains one per line (e.g., terminator.com)
Allowed Email Domains Enter allowed domains one per line
Force Whitelist When enabled, only whitelist domains are accepted
Blocked Emails Enter complete emails one per line

Requirements

  • Magento 2.4.x
  • PHP 8.2+

Installation

Inform the repository in you composer.json

"repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/magenettic/module-email-validation.git"
    }
  ],

Run composer installation

composer require magenettic/module-email-validation

Run the magento CLI commands

bin/magento module:enable Magenettic_EmailValidation
bin/magento setup:upgrade
bin/magento cache:flush

Validation Flow

Customer submits email
    |
    v
Check if email is in blocked emails list
    |
    v
Check if domain is in blocked domains list
    |
    v
Check if Force Whitelist is enabled
    |-- No  --> Allow domain (unless blocked)
    |
    +-- Yes --> Check if domain is in allowed domains list
                  |-- Yes --> Allow email
                  +-- No  --> Reject email

Examples

Scenario 1: Block a specific domain

Blocked Domains: terminator.com
Force Whitelist: No

user@terminator.com --> REJECTED
user@gmail.com      --> ALLOWED

Scenario 2: Allow only specific domains

Allowed Domains: company.com, partner.org
Force Whitelist: Yes

user@company.com    --> ALLOWED
user@partner.org    --> ALLOWED
user@gmail.com      --> REJECTED

Scenario 3: Block specific email

Blocked Emails: spam@spammer.com, test@test.com
Force Whitelist: No

spam@spammer.com    --> REJECTED
test@test.com       --> REJECTED
user@gmail.com      --> ALLOWED

Testing

Unit Tests

The module includes comprehensive unit tests covering:

  • Helper/DataTest.php - Tests for configuration parsing and validation logic
  • Plugin/BeforeEmailValidatorPluginTest.php - Tests for email validation plugin

Run Tests

vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Magenettic/EmailValidation/Test/Unit/

Test Results

27 tests, 61 assertions - OK

Test Coverage

Component Tests Coverage
Helper\Data 18 tests isEnabled, isWhitelistForced, getBlockedDomains, getAllowedDomains, getBlockedEmails, extractDomain, isDomainBlocked, isDomainAllowed, isEmailBlocked
Plugin\BeforeEmailValidatorPlugin 8 tests beforeCreateAccountWithPasswordHash, beforeCreateAccount

Module Structure

Magenettic/EmailValidation/
├── Helper/Data.php                    # Configuration helper
├── Plugin/
│   ├── BeforeEmailValidatorPlugin.php  # Blocks registration
│   └── AroundEmailValidatorPlugin.php  # Global validation
├── Test/Unit/
│   ├── Helper/DataTest.php
│   └── Plugin/BeforeEmailValidatorPluginTest.php
├── etc/
│   ├── adminhtml/system.xml           # Configuration fields
│   ├── di.xml                         # Plugin registration
│   └── module.xml                     # Module declaration
├── composer.json
├── README.md
└── registration.php

License

MIT

About

Allow merchants to block or allow specific email domains and addresses.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages