Email validation rules module for Magento 2.4.x
This module adds custom email validation rules for customer registration, allowing merchants to block or allow specific email domains and addresses.
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.
Stores a list of allowed email domains.
Example: If company.com is in the whitelist, emails like john@company.com will be allowed.
When enabled, only emails from domains in the whitelist will be accepted. All other domains will be blocked.
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.
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 |
- Magento 2.4.x
- PHP 8.2+
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-validationRun the magento CLI commands
bin/magento module:enable Magenettic_EmailValidation
bin/magento setup:upgrade
bin/magento cache:flushCustomer 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
Blocked Domains: terminator.com
Force Whitelist: No
user@terminator.com --> REJECTED
user@gmail.com --> ALLOWED
Allowed Domains: company.com, partner.org
Force Whitelist: Yes
user@company.com --> ALLOWED
user@partner.org --> ALLOWED
user@gmail.com --> REJECTED
Blocked Emails: spam@spammer.com, test@test.com
Force Whitelist: No
spam@spammer.com --> REJECTED
test@test.com --> REJECTED
user@gmail.com --> ALLOWED
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
vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Magenettic/EmailValidation/Test/Unit/27 tests, 61 assertions - OK
| Component | Tests | Coverage |
|---|---|---|
| Helper\Data | 18 tests | isEnabled, isWhitelistForced, getBlockedDomains, getAllowedDomains, getBlockedEmails, extractDomain, isDomainBlocked, isDomainAllowed, isEmailBlocked |
| Plugin\BeforeEmailValidatorPlugin | 8 tests | beforeCreateAccountWithPasswordHash, beforeCreateAccount |
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
MIT