Magento 2:后台创建客户时如何设置邮箱未确认并发送确认邮件?
Got it, let's tackle this problem. By default, Magento 2 automatically marks emails as confirmed when you create a customer from the admin panel—since it trusts admin actions. But if you want all customers (including admin-created ones) to go through email verification before their status is confirmed, here's how you can adjust the behavior:
Why This Happens
Magento's core logic treats admin-created customers as pre-verified, so it sets the is_email_confirmed attribute to 1 (true) immediately upon creation. To override this, we need to intercept the customer save process, force the unconfirmed state, and trigger the confirmation email.
Step-by-Step Solution
We'll build a simple module with a plugin to modify the customer save behavior. Let's assume our module is named Vendor/CustomerEmailVerification (replace Vendor with your actual namespace).
1. Create the Module Configuration
First, set up the module's etc/di.xml to register our plugin:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Customer\Model\Customer"> <plugin name="force_unconfirmed_email_for_admin_customers" type="Vendor\CustomerEmailVerification\Plugin\CustomerSavePlugin" /> </type> </config>
2. Build the Plugin Class
Create Plugin/CustomerSavePlugin.php to intercept the customer save method:
<?php namespace Vendor\CustomerEmailVerification\Plugin; use Magento\Customer\Model\Customer; use Magento\Customer\Model\EmailNotificationInterface; use Magento\Framework\Exception\LocalizedException; class CustomerSavePlugin { private $emailNotification; public function __construct(EmailNotificationInterface $emailNotification) { $this->emailNotification = $emailNotification; } /** * Override admin customer creation to force unconfirmed email and send verification * * @param Customer $subject * @param callable $proceed * @return Customer * @throws LocalizedException */ public function aroundSave(Customer $subject, callable $proceed) { // Check if we're creating a new customer from the admin (store_id = 0) if ($subject->isObjectNew() && $subject->getStoreId() === 0) { // Force email to unconfirmed state $subject->setIsEmailConfirmed(0); // Save the customer first $savedCustomer = $proceed(); // Send the confirmation email to the customer $this->emailNotification->sendConfirmationEmail($savedCustomer); return $savedCustomer; } // Optional: Handle email changes for existing admin customers if (!$subject->isObjectNew() && $subject->getStoreId() === 0) { $originalEmail = $subject->getOrigData('email'); // If email was changed, reset confirmation status and send new email if ($originalEmail && $originalEmail !== $subject->getEmail()) { $subject->setIsEmailConfirmed(0); $savedCustomer = $proceed(); $this->emailNotification->sendConfirmationEmail($savedCustomer); return $savedCustomer; } } // For non-admin or unchanged customers, proceed with default behavior return $proceed(); } }
3. Enable the Module & Deploy Changes
Run these commands from your Magento root directory to activate the module:
bin/magento module:enable Vendor_CustomerEmailVerification bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:clean
How to Test
- Go to Magento Admin → Customers → All Customers → Click Add New Customer.
- Fill in the customer details (including a valid email) and save.
- Check the customer's detail page: The Email Confirmed field should show No.
- The customer will receive a verification email—once they click the link, the status updates to Yes.
This solution ensures that even admin-created customers follow the same email verification flow as frontend-registered ones, keeping your customer data consistent.
内容的提问来源于stack exchange,提问作者Sanjay Sharma

