0

How to Magento 2 Customer Login Notification Email Sent to Customer whenever they login they will get Notification via email

1
  • I have added answer for you. Please check and let me know if you have any query.
    – ZealousWeb
    Mar 9 '21 at 11:01
0

You can send notification with the use of observer. You need to have email template id , sender information and receiver information.

Please follow below steps.

Step 1: Please create events.xml file under path PackageName/Vendor/etc/

<?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_login">
        <observer name="customer_login_observer" instance="PackageName\Vendor\Observer\CustomerLogin" />
    </event>
</config>

Step 2: Please create CustomerLogin.php file under path PackageName/Vendor/Observer/

<?php
namespace PackageName\Vendor\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Area;

class CustomerLogin implements ObserverInterface
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    private $transportBuilder;

    /**
     * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     */
    public function __construct(
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->transportBuilder = $transportBuilder;
        $this->_storeManager = $storeManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        $this->sendEmail($customer);
    }

    public function sendEmail($customer)
    {
        $senderInfo = [
            'name' => $customer->getName(),
            'email' => $customer->getEmail(),
        ];

        $recieverEmail = 'test@zealousweb.com';
        $emailTemplateId = 2;// Here change template id or if you have configuration for email template then pass here

        $emailTemplateVariables = [];
        $emailTempVariables['name'] = $customer->getName();

        $postObject = new \Magento\Framework\DataObject();
        $postObject->setData($emailTempVariables);
        
        $transport = $this->transportBuilder->setTemplateIdentifier($emailTemplateId)
                    ->setTemplateOptions(
                        [
                            'area' => Area::AREA_FRONTEND,
                            'store' => $this->_storeManager->getStore()->getId()
                        ]
                    )
                    ->setTemplateVars(['data' => $postObject])
                    ->setFrom($senderInfo)
                    ->addTo($recieverEmail)
                    ->setReplyTo($recieverEmail)
                    ->getTransport();
        $transport->sendMessage();
    }
}

I have created email template from admin and passed that email template id. Please change and set email template as per your need.

Email Template Content:- https://prnt.sc/10gzmza

{{template config_path="design/email/header_template"}}

<p class="greeting">Dear {{var data.name}},</p>

Please set content according to your need.

{{template config_path="design/email/footer_template"}}
3
  • Have you checked?
    – ZealousWeb
    Mar 10 '21 at 5:54
  • its working thanks Mar 19 '21 at 22:41
  • Can you please approve and upvote this?
    – ZealousWeb
    Mar 22 '21 at 5:40

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.