Magento get email template by code

Magento get email template by code

September 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento get email template by code

Retrieving Magento Email Templates by Code

Magento provides a powerful email template system that allows you to create custom email templates for your store’s communications. If you’ve created an email template for a custom module but are struggling to retrieve it through code, this post will guide you through the steps needed.

Understanding Email Template Configuration in Magento

Firstly, let’s clarify how Magento handles email templates configuration:

  1. You save your email template files under app/locale/en_US/template/email. The file name should match the XML configuration label you specified.
  2. In your module’s config XML (usually located at etc/config.xml), you specify the template details within the <global><template><email></email></template></global> section.

Loading Email Templates by Code

You’ve attempted to load the email template using this code:

$emailTemplate  = Mage::getModel('core/email_template')->loadByCode('custom_template');

However, it returns NULL. The issue lies in how Magento loads and indexes its email templates.

Loading Email Templates Correctly

To correctly load an email template by its code, you should use the following updated approach:

$templateModel = Mage::getSingleton('core/email_template');
$templates = $templateModel->loadDefaultTemplates();

// Loop through all default templates to find your custom one
foreach ($templates as $template) {
    if ($template->getCode() === 'custom_template') {
        $customTemplate = $template;
        break; // Stop the loop once the template is found
    }
}

In this code, we first load all default email templates using loadDefaultTemplates(). Then, we iterate through each template looking for one with a code that matches your custom template. Once found, you can use $customTemplate as needed in your Magento module.

Understanding Email Template Indexing

You mentioned finding the DecryptWeb blog post and trying to load templates using a resource model:

$template_collection =  Mage::getResourceSingleton('core/email_template_collection');

The reason this collection returns empty is that Magento doesn’t index all custom email templates by default. To ensure your template is indexed, you need to trigger the indexing process manually or schedule it in cron.

Indexing Custom Email Templates

To manually index a single custom email template, use the following code:

// Load the specific custom template
$templateId = Mage::getModel('core/email_template')->loadByCode('custom_template')->getId();

// Run the indexing process for this template only
Mage::getSingleton('index/indexer')->processEntityAction('core_email_template', $templateId, 'save');

Remember to replace 'custom_template' with your actual template code.

Conclusion

Retrieving custom email templates in Magento requires a proper understanding of how they are configured and loaded. Always ensure you’ve correctly set up the XML configuration for your templates and that the indexing process has been triggered.

If you find any issues or need further assistance, don’t hesitate to contact our Magento team at Laravel Company for expert consultation. We specialize in Magento development and can help optimize your email template management system.

Extensions Themes Configuration Email

Our network