Hello,
I tried to get involved in Magento modules development by creating this simple module which aims at sending an email notification to an admin address after a new customer completes the registration of his account.
The code is available at: https://github.com/ErikDW1/Magento2_NewCustomersMailNotification.git
I deployed the module into the app/code dir of my Magento installation (2.4develop), however it doesn't work since I don't receive any email after a new customer account registration.
In my setup the customer has to confirm his email address after the registration.
Can you please help me with it?
Solved! Go to Solution.
Hello @erikcapoccd729
Please follow the steps below to create a basic Magento 2 module for notifying admins upon new customer registrations:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'YourNamespace_YourModule', __DIR__ );
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="YourNamespace_YourModule" setup_version="1.0.0"/> </config>
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="customer_register_success"> <observer name="yourmodule_observer_send_email" instance="YourNamespace\YourModule\Observer\SendEmail"/> </event> </config>
<?php namespace YourNamespace\YourModule\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; class SendEmail implements ObserverInterface { protected $_transportBuilder; protected $_inlineTranslation; protected $_scopeConfig; protected $_storeManager; public function __construct( TransportBuilder $transportBuilder, StateInterface $inlineTranslation, ScopeInterface $scopeConfig, StoreManagerInterface $storeManager ) { $this->_transportBuilder = $transportBuilder; $this->_inlineTranslation = $inlineTranslation; $this->_scopeConfig = $scopeConfig; $this->_storeManager = $storeManager; } public function execute(\Magento\Framework\Event\Observer $observer) { $customer = $observer->getEvent()->getCustomer(); // Get admin email address from configuration $adminEmail = $this->_scopeConfig->getValue('trans_email/ident_general/email', ScopeInterface::SCOPE_STORE); $templateOptions = array( 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId() ); $templateVars = array( 'customer' => $customer ); $this->_inlineTranslation->suspend(); $transport = $this->_transportBuilder->setTemplateIdentifier('yourmodule_email_template') ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom('general') ->addTo($adminEmail) ->getTransport(); $transport->sendMessage(); $this->_inlineTranslation->resume(); } }
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9
Hello @erikcapoccd729
Please follow the steps below to create a basic Magento 2 module for notifying admins upon new customer registrations:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'YourNamespace_YourModule', __DIR__ );
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="YourNamespace_YourModule" setup_version="1.0.0"/> </config>
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="customer_register_success"> <observer name="yourmodule_observer_send_email" instance="YourNamespace\YourModule\Observer\SendEmail"/> </event> </config>
<?php namespace YourNamespace\YourModule\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; class SendEmail implements ObserverInterface { protected $_transportBuilder; protected $_inlineTranslation; protected $_scopeConfig; protected $_storeManager; public function __construct( TransportBuilder $transportBuilder, StateInterface $inlineTranslation, ScopeInterface $scopeConfig, StoreManagerInterface $storeManager ) { $this->_transportBuilder = $transportBuilder; $this->_inlineTranslation = $inlineTranslation; $this->_scopeConfig = $scopeConfig; $this->_storeManager = $storeManager; } public function execute(\Magento\Framework\Event\Observer $observer) { $customer = $observer->getEvent()->getCustomer(); // Get admin email address from configuration $adminEmail = $this->_scopeConfig->getValue('trans_email/ident_general/email', ScopeInterface::SCOPE_STORE); $templateOptions = array( 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId() ); $templateVars = array( 'customer' => $customer ); $this->_inlineTranslation->suspend(); $transport = $this->_transportBuilder->setTemplateIdentifier('yourmodule_email_template') ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom('general') ->addTo($adminEmail) ->getTransport(); $transport->sendMessage(); $this->_inlineTranslation->resume(); } }
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9
It appears that the module you created is not working as expected, and you are not receiving email notifications when new customers register for accounts. I would suggest checking the code and ensuring that it is properly configured to send the notifications to the correct email address. Additionally, you may want to check the logs to see if there are any error messages that could be causing the issue.