Add a Custom Email Template in Magento

Within Magento there are a variety of different email templates ready for the merchant to use to send out order and account related emails to the customer. Occasionally, you’ll want to add a custom email template in Magento.

Email templates are stored within the locale/en_US/template/email directory with the sales related emails located within the sales subdirectory.

Magento references these email templates within the relevant module config.xml file, such as the order email template referenced in the Mage_Salesconfig.xml file.

<?xml version="1.0"?>
<config>
    <global>
        ....
        <template>
            <email>
                <sales_email_order_template translate="label" module="sales">
                    <label>New Order</label>
                    <file>sales/order_new.html</file>
                    <type>html</type>
                </sales_email_order_template>
                ....
            </email>
        </template>
        ....
    </global>
    ....
</config>

So if we want to add our very own email template, we’ll have to create our own custom module.

Start with the module’s declaration file.

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

Now add the module configuration file. In this example, the custom email template will be represented by a custom_email_template identifier that will be used to contain information about the email template name, the file name and the type.

<?xml version="1.0"?>
<config>
    <global>
        <template>
            <email>
                <custom_email_template>
                    <label>Custom Email Template</label>
                    <file>custom_email_template.html</file>
                    <type>html</type>
                </custom_email_template>
            </email>
        </template>
    </global>
</config>

For the purpose of this example, the HTML email will contain a heading and for simplicity not contain any custom email variables.

<!--@subject Custom Email Subject @-->


{{template config_path="design/email/header"}}
{{inlinecss file="email-inline.css"}}

<table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td>
            <table cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td class="email-heading">
                        <h1>Welcome to your very first custom email template!</h1>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

{{template config_path="design/email/footer"}}

Within the Magento admin, when heading to System -> Transactional Emails and going to add a new email, you should be able to see the custom email template located in the list of emails to choose to load.

Add a Custom Email Template in Magento

Unfortunately, without specifying a sender name and email within a custom section within the admin’s System -> Configuration area, the email sender name and email address would have to remain hardcoded within the code to send the email.

As this example hasn’t included code to send an example email using this template from within the admin, there is a way to programmatically send the email.

<?php
$emailTemplate = Mage::getModel('core/email_template')
    ->loadDefault('custom_email_template');

$emailTemplateVariables = array();
$emailTemplateVariables['var1'] = 'var 1 value';
$emailTemplateVariables['var2'] = 'var 2 value';
$emailTemplateVariables['var3'] = 'var 3 value';

$emailTemplate->getProcessedTemplate($emailTemplateVariables);

$emailTemplate->setSenderName('Sender Name');
$emailTemplate->setSenderEmail('sender@test.com');

try {
    $emailTemplate->send('recipient@someaddress.com', $emailTemplate->getTemplateSubject(), $emailTemplateVariables);
} catch (Exception $e) {
    echo $e->getMessage();
}

Here we simply use the loadDefault() method of the core email template model, passing in our custom email template identifier that was specified within the module’s config.xml file.

The send() method takes three arguments: the recipient’s email address, the email subject and an array of email variables should you define any.

Note: This article is based on Magento Community/Open Source version 1.9.