0

My Custom Observer is not sending the email.

public function sendAdminMail(Varien_Event_Observer $observer)
{
    $customer = $observer->getEvent()->getCustomer();

    $customerName = $customer->getFirstname() . ' ' . $customer->getLastname();

    $emailTemplate = Mage::getModel('core/email_template');
    $template = "customer_registration_email_template";
    $sender = array('name' => 'Customer Registration',
                    'email' => '[email protected]');
    $recipientEmail = Mage::getStoreConfig('contacts/email/recipient_email');
    $recipientName = "Sales";
    $variables = array('customer'=>$customerName);

    $emailTemplate->sendTransactional(
        $template,
        $sender,
        $recipientEmail,
        $recipientName,
        $variables
    );
}

Here is my config.xml (global node)

<template>
        <email>
            <customer_registration_email_template translate="label" module="adminnotification">
                <label>Customer Registration Admin Notification</label>
                <file>schott/customer_registration.html</file>
                <type>html</type>
            </customer_registration_email_template>
        </email>
    </template>

To be complete, here is the system.xml too

<config>
<label>Customer Registration Admin Notification</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</config>

An email should be sent to the admin when a new customer registers.

What am I doing wrong here? The template is showing in the list of Transactional mails in the backend and it sends when I use it testwise for new orders for example.

Strange is also, that the content of the template does not load in the backend (transactional mails) when I add a new template and want to load its default content (it just loads and stays blank).

Any help appreciated. Thanks in advance!

1 Answer 1

1
      public function sendAdminMail(Varien_Event_Observer $observer)
      {
      $customer = $observer->getEvent()->getCustomer();

      $customerName = $customer->getFirstname() . ' ' . $customer->getLastname();

      $emailTemplate = Mage::getModel('core/email_template')->loadDefault('customer_registration_email_template');
      $senderName = 'Customer Registration';
      $senderEmail = '[email protected]';
      $emailTemplateVariables = array();
      $emailTemplateVariables['customer'] = $customerName;
      $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
      $recipientEmail = Mage::getStoreConfig('contacts/email/recipient_email');
      $recipientName = "Sales";
      $mail = Mage::getModel('core/email')
              ->setToName($recipientName)
              ->setBody($processedTemplate)
              ->setSubject($post_data['subject'])
              ->setFromEmail($senderEmail)
              ->setFromName($senderName)
              ->setType('html')
              ->setToEmail($recipientEmail);

      try{
                   $mail->send();
          }
          catch(Exception $error)
          {
              Mage::getSingleton('core/session')->addError($error->getMessage());
              return false;
          }
      }

Try this code. Not sure other things you have done are correct or not. If you are using any mail senders like mandril then the sender email should be in your domain.

4
  • Thanks but I want to use email templates, so I need a solution with the sendTransactional().
    – Th. Schott
    Commented Nov 28, 2017 at 9:08
  • You can use template in this. Check line no 4 Commented Nov 28, 2017 at 9:27
  • Using this code it sends a mail but the body is empty. Anyway I would prefer using the sendTransactional() as proposed here but I cannot find the problem.
    – Th. Schott
    Commented Nov 28, 2017 at 10:12
  • The problem with the empty body may be due to the way in which you have declared the template in the config.xml. You can use sendTransactional as you said, I am using this script for the custom emails. Commented Nov 28, 2017 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.