cancel
Showing results for 
Search instead for 
Did you mean: 

Basic module for notifying admins upon new customers registrations

SOLVED

Basic module for notifying admins upon new customers registrations

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?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Basic module for notifying admins upon new customers registrations

Hello @erikcapoccd729 

 

Please follow the steps below to create a basic Magento 2 module for notifying admins upon new customer registrations:

  • Create a new directory for your module in app/code, for example app/code/YourNamespace/YourModule
  • Create a registration.php file in your module directory, containing the following code:
    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'YourNamespace_YourModule',
        __DIR__
    );
  • Create a module.xml file in your module directory, containing the following code:
    <?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>
  • Create an events.xml file in your module directory, containing the following code:
    <?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>
  • Create an Observer directory in your module directory, and inside it create a SendEmail.php file, containing the following code:
    <?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();
        }
    }
  • Create an email template in the backend under Marketing > Email Templates, with the identifier "yourmodule_email_template", and customize the content of the email as needed.
  • Enable your module by running the following CLI command from your Magento root directory: php bin/magento module:enable YourNamespace_YourModule

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

View solution in original post

2 REPLIES 2

Re: Basic module for notifying admins upon new customers registrations

Hello @erikcapoccd729 

 

Please follow the steps below to create a basic Magento 2 module for notifying admins upon new customer registrations:

  • Create a new directory for your module in app/code, for example app/code/YourNamespace/YourModule
  • Create a registration.php file in your module directory, containing the following code:
    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'YourNamespace_YourModule',
        __DIR__
    );
  • Create a module.xml file in your module directory, containing the following code:
    <?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>
  • Create an events.xml file in your module directory, containing the following code:
    <?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>
  • Create an Observer directory in your module directory, and inside it create a SendEmail.php file, containing the following code:
    <?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();
        }
    }
  • Create an email template in the backend under Marketing > Email Templates, with the identifier "yourmodule_email_template", and customize the content of the email as needed.
  • Enable your module by running the following CLI command from your Magento root directory: php bin/magento module:enable YourNamespace_YourModule

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

Re: Basic module for notifying admins upon new customers registrations

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.