Send Template Based / Transactional Emails from Magento

Home / Blog / Send Template Based / Transactional Emails from Magento

Very recently, the need arose for us to send out a new, template based email from a custom Magneto module.  A Google search yielded surprising lack of information for this task. Therefore, we documented the process we took to get our module to send out an email based on a template.

Here’s the process…

1.       Create the HTML Template – The email template directory is located in app/locale/en_US/template/email  . The key here is that the {{var sample}} is the syntax for adding variables to the email template. If you need more samples you can take a look some of the sample templates there.

sampleEmail.html

<style type=”text/css”> body { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; } </style>

<div style=”font:11px/1.35em Verdana, Arial, Helvetica, sans-serif;”>

<p>Today’s product of the day is {{var sample}}.  Please visit our store for more details</p>

</div>

2.       Create XML definitions and references to the HTML Template – The sendTransactional method allows you to pass an integer for the id if you’ve added the template via the admin console and would like to use the core_email_template buy yasmin without rx table. However it’s considered best practice to follow the process we’ve laid out and create your template separately and configure them via XML.

Add your Template definitions to your modules config.xml between the <Global> tags

<template>

<email>

<sample_email translate=“label” module=“sample”>

<label>Sample Email</label>

<file>sampleEmail.html</file>

<type>html</type>

</sample_email>

</email>

</template>

In the same config.xml file, add to a reference to the template definition right above the closing </config> tag

<default>

<sample>

<sendemail>

<template>sample_email</template>

</sendemail>

</sample>

</default>

3.       Prep / Call sendTransactional  – In your controller, you’ll need to prep the data and the model necessary for your email.

First, we need to define a reference to your XML email template

const XML_PATH_SAMPLE_EMAIL_TEMPLATE = ‘sample/sendemail/template’;

Here’s where we do the heavy lifting. First we set the Design Config on the email template, and then we call sendTransactional. Something not totally obvious, is the sender must be an array that contains both an ‘email’ and ‘name’ unless your email information is setup in the admin console.

$storeId = Mage::app()->getStore()->getStoreId();

$mailTemplate = Mage::getModel(‘core/email_template’);

$mailTemplate->setDesignConfig(array(‘area’=>’frontend’, ‘store’=>$storeId));

$emailId = Mage::getStoreConfig(self::XML_PATH_SAMPLE_EMAIL_TEMPLATE);

$mailTemplate->sendTransactional(

$emailId,

array(’email’=>’sample@sample.com’, ‘name’=>’Sample Sender’),

$email,

$email,

array(‘sample’ => $sample)

);

For reference, here’s the sendTransactional method from the core/template model.

sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)

Showing 3 comments
  • vishves

    Here ,array(’sample’ => $sample)
    what is sample??

  • tmillhouse

    “sample” can be anything. The sendTransactional method takes an associative array as the fourth parameter (a map), and the email template can reference those items by the name you give them, which in this case is ‘sample’.

  • van230

    Hi there,
    I followed the steps in your article to create and send Transactional Email pro grammatically. But as soon as I created a sampleEmail.html and specified it in config.xml file, my Transactional Email page breaks down. When I click on ‘Add New Template’ the page only shows ‘Load Default Template’ and even that list is empty. So what is it that’s breaking the page?

    Thanks

Leave a Comment