Thursday, 23 January 2014

Send custom email after adding new customer in magento

Introduction:

Here I will explain how to send an email after adding new customer in magento.

Description:

Create you package and register in etc\module\.Browsewire_Notifyusers.xml.

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Browsewire_Notifyusers>
            <active>true</active>
            <codePool>local</codePool>
        </Browsewire_Notifyusers>
    </modules>
</config>


In local as Browsewire and creating Module Notifyusers. In this create two folders called etc & Model.

In etc folder create config.xml and write below code.

<?xml version="1.0"?>
<config>
    <modules>
        <Browsewire_Notifyusers>
            <version>0.0.1</version>
        </Browsewire_Notifyusers>
    </modules>
    <frontend>
        <events>
            <customer_save_after>
                <observers>
                    <Browsewire_Notifyusers_Observer>
                        <type>model</type>
                        <class>Browsewire_Notifyusers/observer</class>
                        <method>customerRegisterSuccess</method>
                    </Browsewire_Notifyusers_Observer>
                </observers>
            </customer_save_after>

        </events>
        <helpers>
            <Browsewire_Notifyusers>
                <class>Browsewire_Notifyusers_Helper</class>
            </Browsewire_Notifyusers>
        </helpers>
    </frontend>
    <global>
        <models>
            <Browsewire_Notifyusers>
                <class>Browsewire_Notifyusers_Model</class>
            </Browsewire_Notifyusers>
        </models>
        <template>
            <email>
                <notify_new_customer module="Browsewire/Notifyusers">
                    <label>Template to notify administrator that new customer is registered</label>
                    <file>notify_new_customer.html</file>
                    <type>html</type>
                </notify_new_customer>
            </email>
        </template>
    </global>
</config>

In Model folder create Notifyusers and Observer for events.

In Notifyusers write below code.

<?php
class Browsewire_Notifyusers extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('Browsewire/Notifyusers');
    }
}

In Observer file write below code.

<?php

class Browsewire_Notifyusers_Model_Observer
{
    public function customerRegisterSuccess(Varien_Event_Observer $observer)
    {
        if (Mage::registry('my_observer_has_run')) {
            return $this;
                }

        Mage::register('my_observer_has_run', true);

        if (!$observer->getCustomer()->getOrigData()) {
                //customer is new, otherwise it's an edit


            $emailTemplate  = Mage::getModel('core/email_template')
                ->loadDefault('notify_new_customer');
            $emailTemplate
                ->setSenderName(Mage::getStoreConfig('trans_email/ident_support/name'))
                ->setSenderEmail(Mage::getStoreConfig('trans_email/ident_support/email'))
                ->setTemplateSubject('New Customer Registered');

            $data = $observer->getCustomer()->getData();

            Mage::log((array)$data);

            $emailTemplateVariables['username']= $data['firstname'].' '.$data['lastname'];
            $emailTemplateVariables['customer_email']   = $data['email'];
            $emailTemplateVariables['password']   = $data['password'];

            $result = $emailTemplate->send(Mage::getStoreConfig('trans_email/ident_general/email'),Mage::getStoreConfig('trans_email/ident_general/name'), $emailTemplateVariables);
        }
    }
}

In locale\en_US\template\email\notify_new_customer.html write below code.



Congratulations, You have successfully registered from Test Magento Site:<br />
Name             : {{var username}}<br />
Email            : {{var customer_email}}<br />
Your Password is : {{var password}}<br/>
...<br />.


Finally go to your magento site and add/create new customer after adding new customer you will receive an email. 




 If you have any issues let me know or leave a comment below.

happy coding.!



No comments:

Post a Comment