Question

I want to send a confirmation mail to the customer when has customer login. Please let me know how can i achieve it. I am using Magento 1.9.2.

AccountController.php

public function loginPostAction()
    {
        if (!$this->_validateFormKey()) {
            $this->_redirect('*/*/');
            return;
        }

        if ($this->_getSession()->isLoggedIn()) {
            $this->_redirect('*/*/');
            return;
        }
        $session = $this->_getSession();

        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            // A smart code to generate OTP PIN.
            $otp_code = strtoupper(bin2hex(openssl_random_pseudo_bytes(3))); 
            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                    if($login)
                    {

                                $customer = Mage::getSingleton('customer/session')->getCustomer();
                                $templateId = 1; // Enter you new template ID
                                $senderName = Mage::getStoreConfig('trans_email/ident_support/name');  //Get Sender Name from Store Email Addresses
                                $senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');  //Get Sender Email Id from Store Email Addresses
                                $sender = array('name' => $senderName,
                                                'email' => $senderEmail);

                        // Set recepient information
                       $recepientEmail = $customer->getEmail();
                      $recepientName = $customer->getName();      

                      // Get Store ID     
                      $store = Mage::app()->getStore()->getId();

                    // Set variables that can be used in email template
                      $vars = array('customerName' => $customer->getName());  


                   // Send Transactional Email
                   Mage::getModel('core/email_template')
                            ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
                  Mage::getSingleton('core/session')->addSuccess($this->__('We Will Contact You Very Soon.'));
                 }



                    $session->login($login['username'], $login['password']);
                    if ($session->getCustomer()->getIsJustConfirmed()) {
                        $this->_welcomeCustomer($session->getCustomer(), true);
                    }
                } catch (Mage_Core_Exception $e) {
                    switch ($e->getCode()) {
                        case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                            $value = $this->_getHelper('customer')->getEmailConfirmationUrl($login['username']);
                            $message = $this->_getHelper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
                            break;
                        case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                            $message = $e->getMessage();
                            break;
                        default:
                            $message = $e->getMessage();
                    }
                    $session->addError($message);
                    $session->setUsername($login['username']);
                } catch (Exception $e) {
                    // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
                }
            } else {
                $session->addError($this->__('Login and password are required.'));
            }
        }

        $this->_loginPostRedirect();

    }
i have created OTP successfully via variable $otp_code and i have mentioned this variable on template id 1. i want to send this template to customer after login. but i am not able to send this template please help me 

Sending mail code

$customer = Mage::getSingleton('customer/session')->getCustomer();
                                $templateId = 1; // Enter you new template ID
                                $senderName = Mage::getStoreConfig('trans_email/ident_support/name');  //Get Sender Name from Store Email Addresses
                                $senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');  //Get Sender Email Id from Store Email Addresses
                                $sender = array('name' => $senderName,
                                                'email' => $senderEmail);

                        // Set recepient information
                       $recepientEmail = $customer->getEmail();
                      $recepientName = $customer->getName();      

                      // Get Store ID     
                      $store = Mage::app()->getStore()->getId();

                    // Set variables that can be used in email template
                      $vars = array('customerName' => $customer->getName());  


                   // Send Transactional Email
                   Mage::getModel('core/email_template')
                            ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
                  Mage::getSingleton('core/session')->addSuccess($this->__('We Will Contact You Very Soon.'));
                 }
Was it helpful?

Solution

I think the error is with your code where you get customer from session. You are trying to get customer by code

$customer = Mage::getSingleton('customer/session')->getCustomer();

But you will not get customer data before login, so you will not get customer email and customer name in your code.

$recepientEmail = $customer->getEmail();
$recepientName = $customer->getName();

Try echoing the data just to confirm. If you are not getting email use

$recepientEmail = $login['username'];

and check again.
EDIT
Replace your email send code with below

$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($login['username']);
    if($customer->getId()){
        $templateId = 1; // Enter you new template ID
        $senderName = Mage::getStoreConfig('trans_email/ident_support/name');
        $senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');
        $sender = array('name' => $senderName,
                        'email' => $senderEmail);
        $recepientEmail = $customer->getEmail();
        $recepientName = $customer->getName(); 
        $store = Mage::app()->getStore()->getId();
        $customer->setOtp($otp_code)->save();
        $vars = array('customerName' => $customer->getName(),'opt_code' => $otp_code);
        Mage::getModel('core/email_template')->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
        Mage::getSingleton('core/session')->addSuccess($this->__('We Will Contact You Very Soon.'));
    }else{
        Mage::getSingleton('core/session')->addError($this->__('There is no registered customer with this email'));
    }

In your email template, use {{var otp_code}} where you want to display OTP

OTHER TIPS

there is a Mage_Customer_Model_Session model's method setCustomerAsLoggedIn() an event customer_login is being dispatched. you can use this event
Add the event to your module's config.xml file (app/code/local/Namespace/Module/etc/config.xml):

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <version>0.1</version>
        </Namespace_Module>
    </modules>
    <global>
        <models>
            <namespace_module>
                <class>Namespace_Module_Model</class>
            </namespace_module>
        </models>
    </global>
    <frontend>
        <events>
            <customer_login>
                <observers>
                    <namespace_module_customer_login>
                        <type>model</type>
                        <class>namespace_module/observer</class>
                        <method>customerLoginEvent</method>
                    </namespace_module_customer_login>
                </observers>
            </customer_login>
        </events>
    </frontend>
</config>

Create a model Namespace_Module_Model_Observer (app/code/local/Namespace/Module/Model/Observer.php). Add a customerLoginEvent() method to the class:

class Namespace_Module_Model_Observer
{

     /**
      * Run couple of 'php' codes after customer logs in
      *
      * @param Varien_Event_Observer $observer
      */
     public function customerLoginEvent($observer)
     {         
         $customer = $observer->getCustomer();

        $templateId = 1; // Enter you new template ID
        $senderName = Mage::getStoreConfig('trans_email/ident_support/name'); //Get Sender Name from Store Email Addresses
        $senderEmail = Mage::getStoreConfig('trans_email/ident_support/email'); //Get Sender Email Id from Store Email Addresses
        $sender = array('name' => $senderName,
        'email' => $senderEmail);

        // Set recepient information
        $recepientEmail = $customer->getEmail();
        $recepientName = $customer->getFirstName() . ' ' . $customer->getLastName();

        // Get Store ID
        $store = Mage::app()->getStore()->getId();

        // Set variables that can be used in email template
        $vars = array('customerName' => $customer->getName());


        // Send Transactional Email
        Mage::getModel('core/email_template')
        ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
     }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top