Sending Magento 2 Customer Registration Emails to Admin
Hello developers! I hope this post finds you in good health and high spirits as we navigate these interesting times together. In today’s article, I want to address a common scenario in Magento 2 development - how to send an email notification to the admin when a customer registers.
The Problem: Sending Admin Emails on Customer Registration
You’ve set up your Magento store, configured payment methods, and even got those beautiful product images looking sharp. But wait, you want to keep track of new customers as they sign up? Sure thing!
The challenge is that out-of-the-box, Magento doesn’t automatically send a copy of the customer registration confirmation email to the admin. We’ll need to add some code to make this happen.
The Solution: Extending Customer Model
To achieve this, we’ll extend the core Magento\Customer\Model\Customer class and override its sendNewAccountEmail method. Here’s how you can do it:
-
Create a custom module (let’s call it
YourCompany_CustomAdminNotifications) in<your-magento-root>/app/code/YourCompany/CustomAdminNotifications -
Inside the
etcfolder, create anevents.xmlfile with the following content:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/events.xsd">
<event name="customer_register_success">
<observer name="yourcompany_customadminnotifications_customer_register_success_observer"
instance="YourCompany\CustomAdminNotifications\Observer\CustomerRegisterSuccess"/>
</event>
</config>
- In the
Observerdirectory, create a file namedCustomerRegisterSuccess.php. This class will be triggered whenever a customer registers successfully.
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
class CustomerRegisterSuccess implements ObserverInterface
{
protected $transportBuilder;
public function __construct(
TransportBuilder $transportBuilder
) {
$this->transportBuilder = $transportBuilder;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$customer = $observer->getCustomer();
if ($customer) {
$templateVars = [
'customer' => $customer,
];
$fromEmail = 'info@yourdomain.com';
$fromName = 'Your Company';
try {
$transport = $this->transportBuilder
->setTemplateIdentifier('customer_new_account')
->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $observer->getStoreId()])
->setTemplateVars($templateVars)
->setFrom($fromEmail, $fromName)
->addTo('admin@example.com', 'Admin')
->getTransport();
$transport->sendMessage();
return true;
} catch (\Exception $e) {
// Log or handle the exception
}
}
return false;
}
}
- Make sure to replace
customer_new_accountwith your actual email template identifier and update theaddTo()method with the admin’s email address.
Conclusion: Sending Admin Emails Made Easy
And there you have it! With this setup, Magento will send a notification email to the specified admin address whenever a new customer registers on your site. Remember to clear cache and run setup upgrade command after saving changes:
php bin/magento setup:upgrade
This solution is a great example of how we can extend Magento’s core functionality using events and observers. Learning these techniques will make you a more effective developer as you work with this powerful e-commerce platform.
Don’t forget to check out Laravel Company for more in-depth tutorials on Magento 2 development, optimization tips, and industry best practices!
Happy coding!