Advanced Transactional Email Templates

Advanced Transactional Email Templates

In this tutorial, we are going to detail of email templates in magento i.e what all features magento offers inside an email template. This tutorial is in continuation of a previous tutorial written on basics of email templates. In the previous tutorial we saw how to create a new email template for a module and sending an email. In this email we will see all the directives and variable assignment method magento offers inside an email template. Email Template Setup I will repeat the steps explained in detail in the previous tutorial, to create a new email template. First we will create our system configuration system.xml


This will add two dropdown in the System -> Configuration -> Customer Configuration -> Account Sharing Options. Magento Email Template DropDown config.xml Next we will create our email template file i.e test.html inside app\locale\en_US\template\email\test.html.



PHP Code To Send Email

public function sendTestEmail($to,$name){
        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $storeId = Mage::app()->getStore()->getId();
        ->setDesignConfig(array('area' => 'frontend', 'store' => $storeId))
        Mage::getStoreConfig(self::XML_PATH_TEST_EMAIL, $storeId),
        Mage::getStoreConfig(self::XML_PATH_TEST_EMAIL_IDENTITY, $storeId),
        array('variable1'=>'Manish','object' => $this,'html'=>'manishtest')

Magento offers many directive to pass dynamic information to email template, so we will see each directive below. All code related to directives is written in class Mage_Core_Model_Email_Template_Filter. var Directive This is used to access variable passed. Syntax for doing this Type1

{{var variable1}}

This access the variable we passed in the array(‘variable1′=>’Manish’) in the sendTransactional() function. So the above code will print Manish in the email template. Type2

{{var object.object_text}}

what this will do is call the function getObjectText() in object ‘object’. The object has been passed in the array() as

array('variable1'=>'Manish','object' => $this)

So in the same class from where the sendTransactional() is called, if we create a new method called getObjectText(), the value returned by this function will be shown in the email.

public function getObjectText(){
        return 'This is my text';

Another extension of the above which is more commonly used is, suppose we have many variables to pass to an email template e.g the $_POST variable. So to do this in the array we will pass

array('object'=>new Varien_Object($_POST))

and in our email template we can access this by

{{var object.post_var1}}

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Youtube

leave a comment