Magento: Sending emails from backend

Email is one of the most popular marketing tools in e-commerce. Almost of this work is done automatically by the server. In this tutorial, I'm going to show you how to send emails from backend in Magento.

Of course, Magento provides us the class for this duty. It is Mage_Core_Model_Email_Template. Let's examine it with me.

class Mage_Core_Model_Email_Template extends Mage_Core_Model_Template


abstract class Mage_Core_Model_Template extends Mage_Core_Model_Abstract



You can treat Mage_Core_Model_Email_Template as a model. So, you can call it as normal model:

$email_template = Mage::app()->getModel('core/email_template');

And also load it by template_id in table core_email_template:

Tale core_email_template Table core_email_template

This table contains all new email templates create from admin page, but NOT core template or your new email templates you created from backend when developing module. There is a confusion here. These template (core templates, own templates) should be load by function loadDefault($templateId, $locale=null) of class Mage_Core_Model_Email_Template. I have summarized that there are 2 kinds of template_id (or called template_id by Magento):

- template_id from table core_email_template (ex: "1", "2",... ). They are incremental number of this table) and they should be loaded by function load

- template_id from config.xml file in your module(ex: "admin_emails_forgot_email_template"). They should be loaded by function loadDefault

In easy way, I can use function sendTransactional,  instead of function send you can see the following code this this function:

public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)
  if (is_numeric($templateId)) {
  } else {
    $localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
    $this->loadDefault($templateId, $localeCode);

There is conjunction between the template template_id in your config.xml file and the default value in system.xml. Here is an example:



    <section_group_field module="yourmodule" translate="label">
      <label>Your module</label>

Be careful with tag <section_group_field>. This tag is the conjunction. You can see it in function getDefaultTemplates()

static public function getDefaultTemplates()
  if(is_null(self::$_defaultTemplates)) {
    self::$_defaultTemplates = Mage::getConfig()->getNode(self::XML_PATH_TEMPLATE_EMAIL)->asArray();

  return self::$_defaultTemplates;

There is some useful functions for you when debugging. One of them is isValidForSend().

public function isValidForSend()
  return !Mage::getStoreConfigFlag('system/smtp/disable')
    && $this->getSenderName()
    && $this->getSenderEmail()
    && $this->getTemplateSubject();