Customer retention is fundamental for any Ecommerce operation and website registration is a great way to accommodate needs of repeat customers and building long term relationships. Magento sends registration Email to customers when they sign up or register. But what about admin or store owner? It doesn’t notify admin for new registration and thus store owner stays uninformed about this key event or he has to check the admin panel frequently which wastes time. Today we have come up with custom code to send Email to admin after customer registration in Magento 2.
Sending Email notification to admin after customer registration simply requires to create an event & observer to serve your need.
Let’s see the steps to send Email to admin after customer registration in Magento 2
Create events.xml file in \app\code\Namespace_Modulename\etc\frontend\ and add following code. Here, we need to define an event for Send mail after customer registration.
1 |
<!--?xml version="1.0"?--> |
Create SendMailToAdmin.php file in \app\code\Namespace_Modulename\Observer\. This observer class is used to send mail after customer is registered successfully.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php namespace Namespace_Modulename\Custom\Observer; use Magento\Framework\Event\ObserverInterface; class SendMailToAdmin implements ObserverInterface { const XML_PATH_EMAIL_RECIPIENT = 'trans_email/ident_general/email'; protected $_transportBuilder; protected $inlineTranslation; protected $scopeConfig; protected $storeManager; protected $_escaper; public function __construct( \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Escaper $escaper ) { $this->_transportBuilder = $transportBuilder; $this->inlineTranslation = $inlineTranslation; $this->scopeConfig = $scopeConfig; $this->storeManager = $storeManager; $this->_escaper = $escaper; } public function execute(\Magento\Framework\Event\Observer $observer) { $customer = $observer->getData('customer'); $this->inlineTranslation->suspend(); try { $error = false; $sender = [ 'name' => $this->_escaper->escapeHtml($customer->getFirstName()), 'email' => $this->_escaper->escapeHtml($customer->getEmail()), ]; $postObject = new \Magento\Framework\DataObject(); $postObject->setData($sender); $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; $transport = $this->_transportBuilder ->setTemplateIdentifier('1') // Send the ID of Email template which is created in Admin panel ->setTemplateOptions( ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, // using frontend area to get the template file 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,] ) ->setTemplateVars(['data' => $postObject]) ->setFrom($sender) ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope)) ->getTransport(); $transport->sendMessage(); ; $this->inlineTranslation->resume(); } catch (\Exception $e) { \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug($e->getMessage()); } } } |
Creating custom code to send Email notification to admin after customer registration is not a tough cookie to crack. You need to simply follow the steps above and you are done. Let me know if you stuck somewhere or have any queries through commenting, I would be glad to help you!
Hello,
I used your code to send the registration e-mail to admin.
The e-mail is sent without any error, but in luma theme- is it possible to change it to my custom theme (the luma logo is only shown in the e-mail to admin).
Where can i define the admin e-mail address, addressed by your code?
I don´t want the e-mail to be sent to the same address as the automatic generated invoice e-mails.
Kind regards
MMM
“addTo” this is the field where you can mention receivers means admin email address.
What about sending an email to the admin after a customer submits a return request?
Which return request you are talking about? I mean are you talking about third party extension or something?
Hi, I have enabled RMA in the admin panel.
Did anyone manage to get it to work? It’s still not working with me.
What error you are facing here?
Did anyone manage to get it to work? It’s still not working with me.
I created the module and installed it… Also created the email template. But I didn’t get the email. I registered an new account and got the regular welcome email. But I didn’t get the additional email.
How do I trouble shot the code?
Kevin
Confirm your extension is installed and enabled and you have implemented the code properly also check that your code is executing.
I created the module and installed it… Also created the email template. But I didn’t get the email. I registered an new account and got the regular welcome email. But I didn’t get the additional email.
How do I trouble shot the code?
Kevin
Hello,
I’ve done as you mentioned above, but it seems not to be working.
please advise would be appreciated,
regards
Can you please tell us if you are facing any error?
Please confirm your other emails are working fine, like registration and order confirmation.
Hi,
thank you for responding,
yes the emails are working correct and I’m not facing any errors.
regards
Hello,
I’ve done as you mentioned above, but it seems not to be working.
please advise would be appreciated,
regards
Can you please tell us if you are facing any error?
Please confirm your other emails are working fine, like registration and order confirmation.
Hi,
thank you for responding,
yes the emails are working correct and I’m not facing any errors.
regards
Module not found error
Kindly help
it’s an event not a module. you don’t need to activate it from the terminal.
did it work with you?
I was trying to active this module from ssh.Now it is activated but not working.
You need to create custom extension first, then implement this code into that.
Module not found error
Kindly help
it’s an event not a module. you don’t need to activate it from the terminal.
did it work with you?
Hello! Thank you for posting.
I tried your code, unfortunately, it’s not working with me. is there a way i can hard code the email it’s to send to?
Thank you!
Can you tell me, which error you are facing while trying this code?
There is no error, but no emails are being sent.
Did you check that you have set proper email template Id in line no 51.
->setTemplateIdentifier('1') // Send the ID of Email template which is created in Admin panel
Instead of 1 you need to pass your email template id and make sure, your other emails are working fine.
Hello! Thank you for posting.
I tried your code, unfortunately, it’s not working with me. is there a way i can hard code the email it’s to send to?
Thank you!
Can you tell me, which error you are facing while trying this code?
There is no error, but no emails are being sent.
Did you check that you have set proper email template Id in line no 51.
->setTemplateIdentifier('1') // Send the ID of Email template which is created in Admin panel
Instead of 1 you need to pass your email template id and make sure, your other emails are working fine.