1

send an email to Admin when a User logins in frontend into his account. magento 2, using event and observer

2

Ok. So here it is.

                 // CODE NOT TESTED.

Your event is customer_customer_authenticated. And the dispatched method is this.

$this->eventManager->dispatch(
            'customer_customer_authenticated',
            ['model' => $customerModel, 'password' => $password]
        );

Create a file inside the given path app/code/Namespace/Modulename/etc/frontend/events.xml. Add the below code inside that

<?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_customer_authenticated">
       <observer name="customer_customer_authenticated_email" instance="Namespace\Modulename\Observer\SendEmailAfterLogin" />
   </event>
</config>

After that create a file again in this path app/code/Namespace/Modulename/Observer/SendEmailAfterLogin.php. And the code will be this

<?php
namespace Namespace\Modulename\Observer;

class SendEmailAfterLogin implements \Magento\Framework\Event\ObserverInterface
{
  public function execute(\Magento\Framework\Event\Observer $observer)
  {
     // send email from here//
     // If you want to load template by id then try to do this.
     // Use Magento\Email\Model\Template; Create the constructor as per that 
     // and use loadDefault($templateId) function to load a template. 
  }
}
  • Updated the function to give you an idea. Its not a full code but you will get the idea how to do that. – Kingshuk Deb Apr 10 '17 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.