Pawan_oCodewire
Pawan_oCodewire

Reputation: 216

Email Notification when a new customer has been added - 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

Upvotes: 4

Views: 16124

Answers (7)

Ketan Borada
Ketan Borada

Reputation: 864

This is code for sending new customer email to Admin also
Override file
\app\code\core\Mage\Customer\Model\Customer.php
to local
\app\code\local\Mage\Customer\Model\Customer.php

Replace below function

protected function _sendEmailTemplate($template, $sender, $templateParams = array(), $storeId = null)
    {
        /** @var $mailer Mage_Core_Model_Email_Template_Mailer */
        $mailer = Mage::getModel('core/email_template_mailer');
        $emailInfo = Mage::getModel('core/email_info');
        $emailInfo->addTo($this->getEmail(), $this->getName());
        $mailer->addEmailInfo($emailInfo);

        // Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig($sender, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
        $mailer->setTemplateParams($templateParams);
        $mailer->send();
        return $this;
    }

to

protected function _sendEmailTemplate($template, $sender, $templateParams = array(), $storeId = null)
    {
        /** @var $mailer Mage_Core_Model_Email_Template_Mailer */
        $mailer = Mage::getModel('core/email_template_mailer');
        $emailInfo = Mage::getModel('core/email_info');
        $emailInfo->addTo($this->getEmail(), $this->getName());

        if($template="customer/create_account/email_template"){

            $emailInfo->addBcc(Mage::getStoreConfig('trans_email/ident_general/email'), $this->getName());
              //Add email address in Bcc you want also to send
        }

        $mailer->addEmailInfo($emailInfo);


        // Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig($sender, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId(Mage::getStoreConfig($template, $storeId));
        $mailer->setTemplateParams($templateParams);
        $mailer->send();
        return $this;
    }

Upvotes: 0

Bikram Shrestha
Bikram Shrestha

Reputation: 2070

You can try this extension which Get a notification email of every new customer registration, including a customizable email template. http://www.magentocommerce.com/magento-connect/customer-registration-notification.html

Upvotes: 0

Bikram Shrestha
Bikram Shrestha

Reputation: 2070

You can try this extension which Get a notification email of every new customer registration, including a customizable email template. http://www.magentocommerce.com/magento-connect/customer-registration-notification.html

Upvotes: 0

DuffyBelfield
DuffyBelfield

Reputation: 68

You could extend Mage/Customer/Resource/Customer.php - protected function _beforeSave(Varien_Object $customer)

if ($result) {
   throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('This customer email already exists'), Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS);
} else {
    // SEND EMAIL - Use a custom template 
}

Upvotes: 0

alphacentauri
alphacentauri

Reputation: 662

It could be done perfectly with Magento event/ observer system. First of all, register your module.

app/etc/modules/Namespace_Modulename.xml

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

Than write a config file for it.

app/code/local/Namespace/Modulename/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>0.0.1</version>
        </Namespace_Modulename>
    </modules>
    <frontend>
        <events>
            <customer_register_success>
                <observers>
                    <unic_observer_name>
                        <type>model</type>
                        <class>unic_class_group_name/observer</class>
                        <method>customerRegisterSuccess</method>
                    </unic_observer_name>
                </observers>
            </customer_register_success>
        </events>
        <helpers>
            <unic_class_group_name>
                <class>Namespace_Modulename_Helper</class>
            </unic_class_group_name>
        </helpers>
    </frontend>
    <global>
        <models>
            <unic_class_group_name>
                <class>Namespace_Modulename_Model</class>
            </unic_class_group_name>
        </models>
        <template>
            <email>
                <notify_new_customer module="Namespace_Modulename">
                    <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>

Here is a few things happened:

  1. A new observer was registered to fire on event customer_register_success (it's dispatched at line 335 in Mage_Customer_AccountController) in frontend/events node. It's better than using customer_save_after, because the last one will fire every time customer is saved, not only when he is registered;
  2. A new email template was registered in global/template/email node. To allow us to send a custom email with it.

Next create an email template (file).

app/locale/en_US/template/notify_new_customer.html

Congratulations, a new customer has been registered:<br />
Name: {{var name}}<br />
Email: {{var email}}<br />
...<br />

After that define an observer method.

app/code/local/Namespace/Modulename/Model/Observer.php

class Namespace_Modulename_Model_Observer
{
    public function customerRegisterSuccess(Varien_Event_Observer $observer)
    {
        $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');
        $result = $emailTemplate->send(Mage::getStoreConfig('trans_email/ident_general/email'),(Mage::getStoreConfig('trans_email/ident_general/name'), $observer->getCustomer()->getData());
    }
}

EDIT: as @benmarks pointed out this solution will not work if customer is registered during checkout. The solution to this behavior is described here. But, I think, it's better to use _origData functionality as @benmarks suggested. So use his answer as guideline to achieve what you need.

Useful links:

Upvotes: 5

Ashley Schroder
Ashley Schroder

Reputation: 3886

As an alternative to an event based approach, you could run a separate API based script to fetch new (or updated) customers and email them to you, it may or may not be desirable for you to get a once-per-day list rather than an email for every single customer too.

Benefits:

  • Nothing installed in your Magento store
  • Does not add any extra processing or network delays to a new/updated customer operation
  • Opportunity to batch all emails for a day into one email

Here's an example I used recently which is almost exactly what you want, which is why this caught my eye. Code is available here.

$client =
   new SoapClient('http://www.yourstore.com/magento/api/soap?wsdl');
 $session = $client->login('TEST_USER', 'TEST_PASSWORD');

 $since = date("Y-m-d", strtotime('-1 day'));
 // use created_at for only new customers
 $filters = array('updated_at' => array('from' => $since)); 


 $result = $client->call($session, 'customer.list', array($filters));

 $email = "New customers since: $since\n";

 foreach ($result as $customer) {
         $email .= $customer["firstname"] ." ".
                     $customer["lastname"] . ", " .
                     $customer["email"] . "\n";
 }

mail("[email protected]", "Customer report for: $since", $email);

Upvotes: 1

benmarks
benmarks

Reputation: 23205

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.

Upvotes: 7

Related Questions