Email Confirmation for Specific Domain Users in Magento
As an experienced Magento developer, I often receive requests to modify user flow based on specific criteria. One of the most common is enabling email confirmation only for users from certain domains while keeping it disabled for others.
In this post, I’ll guide you through implementing email confirmation and group assignment specifically for users with a “foo.com” domain.
Understanding the Email Confirmation Process
Before we start, let’s briefly recap how email confirmation works in Magento. By default, all new user registrations require email confirmation via a link sent to their provided email address.
When enabled, Magento adds two columns to the customer_entity_varchar table: confirmation_link and confirmation_key. These are used to create a unique confirmation URL for each user.
Disabling Global Confirmation
First, you need to disable global email confirmation. Navigate to Stores > Configuration > Customers > Customer Creation Options.
Set “Enable Email Confirmation” to “No”. Click “Save Config”.
Now, let’s move on to the customization process.
Creating a Custom Observer for Email Confirmation
We’ll create a custom observer that triggers when a new customer is created. This observer will check if the user’s domain is “foo.com”. If yes, it will generate and save the confirmation link/key pair.
- Create a module using Magentoâs module creator tool or manually in your app/code directory following the structure:
your_module
âââ etc
âââ frontend.xml
âââ Observer
âââ CustomerSaveAfter.php
- In
Observer/CustomerSaveAfter.php, implement the observer method:
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\ResourceModel\Customer;
use Magento\Framework\App\RequestInterface;
class CustomerSaveAfter implements ObserverInterface
{
protected $customerResource;
protected $request;
public function __construct(
Customer $customerResource,
RequestInterface $request
) {
$this->customerResource = $customerResource;
$this->request = $request;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$customerId = $observer->getCustomer()->getId();
if (empty($customerId)) return;
$email = $this->request->getParam('email');
$domain = explode('@', $email)[1];
if ($domain !== 'foo.com') return;
// Generate confirmation link and save
$confirmationCode = substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 5)), 0, 20);
$this->customerResource->saveAttribute($customerId, 'confirmation_link', $confirmationCode);
// Save confirmation key (not used in our case)
$this->customerResource->saveAttribute($customerId, 'confirmation_key', md5(time()));
}
}
- Add the observer to
etc/frontend.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<event name="customer_save_after">
<observer instance="your_module\Observer\CustomerSaveAfter"
method="execute"/>
</event>
</config>
Sending the Confirmation Email
Magento automatically sends an email if the confirmation_link is present. You can customize this by overriding the template at:
app/design/frontend/your_package/your_theme/Magento_Customer/templates/customer/new_account.xml
Add your custom confirmation link and any group assignment logic using Magentoâs customer API.
Assigning Users to a Specific Group
To assign confirmed users to a specific group (let’s call it “foo_group”), you can create another observer for the customer_save_after event to check if the user has confirmed their email address by verifying the presence of the confirmation_link.
In your custom module:
-
Create an observer file:
Observer/CustomerSaveAfterGroup.php -
Implement the observer method:
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\ResourceModel\Customer;
class CustomerSaveAfterGroup implements ObserverInterface
{
protected $customerResource;
protected $groupFactory; // Inject Magento\Customer\Model\GroupFactory
public function __construct(
Customer $customerResource,
\Magento\Customer\Model\GroupFactory $groupFactory
) {
$this->customerResource = $customerResource;
$this->groupFactory = $groupFactory;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$customerId = $observer->getCustomer()->getId();
$confirmationLink = $this->customerResource->getAttributeRawValue($customerId, 'confirmation_link');
if (!empty($confirmationLink)) {
// Get or create the "foo_group" group
$group = $this->groupFactory->create()->loadByCode('foo_group');
if (empty($group->getGroupId())) {
$group->setCode('foo_group')->save();
}
// Assign customer to 'foo_group'
$this->customerResource->assignCustomerToGroup($customerId, $group->getGroupId());
}
}
}
Conclusion
In this post, we explored how to customize Magento’s email confirmation process for specific domains. By creating a custom observer and leveraging Magentoâs built-in customer API, you can efficiently manage user registration based on domain-specific criteria.
Remember to flush your cache after implementing these changes.
For more advanced techniques in Magento development, consider checking out our comprehensive guides at LaravelCompany.
Happy coding!