Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email Notification when a new customer has been added - Magento

Tags:

magento

I would like to send an email notification to my store's contact email address every time when a new customer has been Registered.

I don't want to purchase any kind of the extensions, so please help me to do this

Thanks in advance

like image 967
Pawan_oCodewire Avatar asked Dec 12 '22 00:12

Pawan_oCodewire


1 Answers

Best practice is to use Magento's event system.

app/etc/modules/Your_Module.xml

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

app/core/local/Your/Module/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <global>
        <models>
            <your_module>
                <class>Your_Module_Model</class>
            </your_module>
        </models>
    </global>
    <frontend>
        <events>
            <customer_save_after>
                <observers>
                    <your_module>
                        <type>model</type>
                        <class>your_module/observer</class>
                        <method>customerSaveAfter</method>
                    </your_module>
                </observers>
            </customer_save_after>
        </events>
    </frontend>
</config>

app/code/local/Your/Module/Model/Observer.php

<?php

class Your_Module_Model_Observer
{
    public function customerSaveAfter(Varien_Event_Observer $o)
    {
        //Array of customer data
        $customerData = $o->getCustomer()->getData();

        //email address from System > Configuration > Contacts
        $contactEmail = Mage::getStoreConfig('contacts/email/recipient_email');

        //Mail sending logic here.
        /*
           EDIT: AlphaCentauri reminded me - Forgot to mention that
           you will want to test that the object is new. I **think**
           that you can do something like:
        */
        if (!$o->getCustomer()->getOrigData()) {
            //customer is new, otherwise it's an edit 
        }
    }
}

EDIT: Note the edit in the code - as AlphaCentauri pointed out, the customer_save_after event is fired for both inserts and updates. The _origData conditional logic should allow you to incorporate his mailing logic. _origData will be null.

like image 146
benmarks Avatar answered May 12 '23 05:05

benmarks