Email notification when adding a new customer - Magento

I would like to send an email notification to the email address of my store every time a new customer is registered.

I do not want to buy any extensions, so please help me do this

Thank you in advance

+4
source share
7 answers

Best practice is to use the Magento event system.

applications / 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> 

application / kernel / 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> 

Application / 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 an edit } } } 

EDIT: Pay attention to code editing - as AlphaCentauri pointed out, the customer_save_after event is fired for both inserts and updates. The conditional logic _origData should allow you to enable your mail logic. _origData will be null .

+7
source

This can be done perfectly with the Magento event / observer system. First of all, register your module.

applications / 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> 

Write the configuration file for it.

Application / 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 are a few things:

  • A new observer was registered to trigger the customer_register_success event (it was sent on line 335 in the Mage_Customer_AccountController ) in the frontend/events node. This is better than using customer_save_after , since the latter will fire every time a client is saved, and not just when it is registered;
  • A new email template has been registered in global/template/email node. To allow us to send a special email with him.

Next, create an email template (file).

application / 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 /> 

Then determine the observer method.

Application / 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: because @benmarks indicated that this solution will not work if the client is registered during the validation. A solution to this behavior is described here . But, I think it is better to use the _origData functionality as suggested by @benmarks. Therefore, use it as a guide to achieve what you need.

Useful links:

+5
source

As an alternative to the event-based approach, you can run a separate script-based API to retrieve new (or updated) clients and send them by e-mail to you, maybe or not, per day, and not by e-mail for each individual client.

Benefits:

  • Nothing is installed in your Magento store.
  • Does not add additional processing or network latency to a new / updated client operation.
  • Ability to send all emails in one day to one email address.

Here is an example that I used recently, and this is almost what you want, and that is why it caught my attention. The code is available here .

 $client = new SoapClient('http://www.yourstore.com/magento/api/soap?wsdl'); $session = $client->login('TEST_USER', 'TEST_PASSWORD'); $since = date("Ymd", 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); 
+1
source

You can 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 } 
0
source

You can try this extension, which receives an email notification of each new client registration, including a custom email template. http://www.magentocommerce.com/magento-connect/customer-registration-notification.html

0
source

You can try this extension, which receives an email notification of each new client registration, including a custom email template. http://www.magentocommerce.com/magento-connect/customer-registration-notification.html

0
source

This is the code for sending a new client email to the administrator as well
Override file
\app\code\core\Mage\Customer\Model\Customer.php


\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
source

All Articles