Magento – email template file configuration

email-templatesmagento-1.9

I am creating a custom module in Magento which send emails. I've created the email template in the "transactional email templates" section in the backend, and it works.

Now I would like to add the email template in the files of Magento to have a durable solution (currently, if I reset the database configuration, my template will be removed). I can't find a way to load the template.

  1. I've created my template file in app/locale/en_GB/template/email/notification.html
  2. I've tried to declare this template in my config.xml

    <template>
        <email>
            <vendor_report_notification_email_template translate="label" module="namespace_ordersReport">
                <label>Vendor Report Notification Template</label>
                <file>notification.html</file>
                <type>html</type>
            </vendor_report_notification_email_template>
        </email>
    </template>
    

I can't find how to add an email template and how to load it to 1. be able to send an actual email, 2. customize it in the backend if needed through the load template function of the transactional emails.

Best Answer

You will need to add some fields to the system.xml

[...]
<!-- sender -->
<sender_email_identity translate="label">
    <label>Email Sender</label>
    <frontend_type>select</frontend_type>
    <source_model>adminhtml/system_config_source_email_identity</source_model>
    <sort_order>20</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</sender_email_identity>

<!-- template -->
<email_template translate="label">
    <label>Email Template</label>
    <frontend_type>select</frontend_type>
    <source_model>adminhtml/system_config_source_email_template</source_model>
    <sort_order>30</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</email_template>
[...]

And retrieve and set the data where ever you want to send it from

$templateData = new Varien_Object();
$templateData->setData(array(
    'varname' => 'value',
    'varname' => 'value',
));

$translate = Mage::getSingleton('core/translate');
$translate->setTranslateInline(false);

$mailTemplate = Mage::getModel('core/email_template');
$mailTemplate->setDesignConfig(array('area' => 'frontend'))
    ->sendTransactional(
        Mage::getStoreConfig('path/of/email_template'),
        Mage::getStoreConfig('path/of/sender_email_identity'),
        'recipient@example.com',
        null,
        array('data' => $templateData)
    );

if (!$mailTemplate->getSentSuccess()) {
    throw new Exception();
}

$translate->setTranslateInline(true);