0

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.'));
                 }
6
  • i want to send OTP to customer after successful login. i have created OTP successfully but i am not able to send OTP while i have added OTP to email template. Feb 10, 2017 at 9:41
  • Is there any specific errors you are facing? Put Mage::getModel('core/email_template')->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId); in try catch and check for errors. Feb 10, 2017 at 9:50
  • i did not get any error Feb 10, 2017 at 9:57
  • You can send other emails, correct? Feb 10, 2017 at 10:00
  • after login i am getting my success message "we will contact you very soon " Feb 10, 2017 at 10:01

2 Answers 2

2

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

9
  • thank you so much you saved me. i am very happy ...thanks once again Feb 10, 2017 at 11:22
  • No Problem @ErSarveshVTiwari, If the answer has resolved your problem just make sure you mark is as correct. It will help others in the future. Thanks. Feb 10, 2017 at 11:26
  • i have marked useful answer bro ..... Feb 10, 2017 at 11:27
  • bro one thing more after login i have created otp i want to add otp in email template Feb 10, 2017 at 11:27
  • and also save otp in table Feb 10, 2017 at 11:27
0

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);
     }

}
9
  • i tried this but i did not get anything Feb 10, 2017 at 9:56
  • checked twice , it should work , what you don't get ? Feb 10, 2017 at 10:04
  • it does not response . it does not send mail after login Feb 10, 2017 at 10:09
  • share your code of sending mail Feb 10, 2017 at 10:10
  • please check my updated question Feb 10, 2017 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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