Magento comes with a list of default email templates for various scenarios like orders, shipments, customer interactions etc., If you would like to customize email templates in Magento whether its modifying the HTML/plain text within a template or adding dynamic content blocks, below are few handy notes.

Email templates location

The default directory where email templates are located in Magento is at app/locale/en_US/template/email/. Note that these templates are not intended to be customized via the code base.

Overriding default email templates

In order to customize an email template, navigate to Magento Admin > System > Transactional Emails. Click on Add New Template and in the following page, pick the email template that you are wanting to customize, select an appropriate locale and click on Load Template.
new-email-template-magento-e1419547152139

 

This should populate the template content and template styling sections. Customize the template’s content and styling to suit your needs. Subsequently, navigate to System > Configuration > Customers > Customer Configuration > Create New Account Options (tab) > Default Welcome email.

custom-welcome-email-template-magento-e1419574586537

Pick the newly created custom email template from the dropdown and Save Config. Moving forward, this customized email template would be used instead of the default template in app/locale directory.

Note: it may seem logical to copy a email template from the app/locale directory to app/design/frontend/<package>/<theme>/locale/en_US/template/email/—- and expect it to work as an override for the default template; unfortunately, that’s not the case.

System variables

There are a wide range of system variables available for use in email templates. These variables are replaced by the corresponding dynamic content; very handy to avoid redundant text across email templates.

Examples include:

  • store web address: {{store url=""}}
  • store name: {{var store.getFrontendName()}}

Some of the system variables are global like the above two examples whereas others are specific to an email template. For instance, review the order confirmation email template:

app/locale/en_US/template/email/sales/order_new_guest.html

Notice the first few lines of this file with the vars annotation. Variables like {{var order.increment_id}} are readily available for use within the transnational email templates.

Please find the list of these system variables available in this knowledge base entry.

Custom variables

Magento enables you to use custom text or HTML via custom variables. To create a custom variable, navigate to Magento Admin > System > Custom Variables.

Let’s say that you have created a simple custom variable with text “happy holidays”.  To insert a custom variable within a email template, open an email template and click on Insert Variable. Notice “holiday-message” under Custom Variables. This gives you the ability to easily swap text of HTML across multiple email templates.

custom-variables-magento-e1419549044754

Adding dynamic content

Let’s say that when a customer registers on the site, apart from using text/HTML in the welcome email, you would like to add dynamic content about your site’s latest product offerings: for instance, let’s add three latest products of type “simple” from the site to the welcome email sent out upon customer registration.

One way to achieve this is to create a custom module with a block and call this block type with the email template.

The first step is to create the bootstrap file for the custom module.
app/etc/modules/Sri_Custom.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Sri_Custom>
            <active>true</active>
            <codePool>local</codePool>
        </Sri_Custom>
    </modules>
</config>

Next, create the config.xml file for this custom module.
app/code/local/Sri/Custom/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Sri_Custom>
            <version>0.1.0</version>
        </Sri_Custom>
    </modules>
    <global>
        <blocks>
            <custom>
                <class>Sri_Custom_Block</class>
            </custom>
        </blocks>
    </global>
</config>

Next, let’s create a block with a function which returns the product collection we need for this case.
app/code/local/Sri/Custom/Block/Latestproducts.php

<?php
class Sri_Custom_Block_Latestproducts extends Mage_Core_Block_Template
{
    public function getLatestProducts()
    {
    	$collection = Mage::getResourceModel('catalog/product_collection');
		$collection->addAttributeToSort('entity_id', 'DESC')
		->addAttributeToFilter('type_id', 'simple')
		->getSelect()->limit(3);

		return $collection;
    }   
}

app/design/frontend/package/theme/template/email/latestproducts.phtml
Lastly, the template file which generates the HTML to display the product information within the email template.

<h3>Checkout our latest products</h3>
<ul>
<?php
foreach($this->getLatestProducts() as $item) {
	$product = Mage::getModel('catalog/product')->load($item->getEntityId());
	echo '<li><a href="' . $product->getProductUrl() . '">' . $product->getName() . '</a></li>';
} ?>
</ul>

This custom block can be used within the email template as

{{block type="custom/latestproducts" area="frontend" template="email/latestproducts.phtml"}}

The result would be something like,

latest-products-in-magento-email-template-e1419575248569

Switching content blocks based on the store

In cases where an email template is used across multiple stores, there can be specific content blocks within the email template that depend on the store. For instance, in the customer welcome email template, the welcome message could be specific to the store like “Welcome to store one” / “Thanks for registering on store two”.

AppEmulation can be used to set the appropriate store environment. Below is an example from Magento core of it’s usage:

app/code/core/Mage/Sales/Model/Order.php
public function sendNewOrderEmail() {
.....
// Start store emulation process
        $appEmulation = Mage::getSingleton('core/app_emulation');
        $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
......
// Stop store emulation process
        $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
}