Email Notification when a new customer has been added - Magento

后端 未结 7 1030
鱼传尺愫
鱼传尺愫 2021-01-03 06:31

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 kin

相关标签:
7条回答
  • 2021-01-03 07:03

    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

    0 讨论(0)
  • 2021-01-03 07:13

    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.

    0 讨论(0)
  • 2021-01-03 07:14

    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

    0 讨论(0)
  • 2021-01-03 07:22

    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:

    • Magento custom emails • Inchoo
    • Magento - Wiki - Customize Magento using Event/Observer
    0 讨论(0)
  • 2021-01-03 07:22

    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("customer-manager@yourstore.com", "Customer report for: $since", $email);
    
    0 讨论(0)
  • 2021-01-03 07:22

    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;
        }
    
    0 讨论(0)
提交回复
热议问题