Loading a Magento mail template and fill its vars from code?

Loading a Magento mail template and fill its vars from code?

August 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Loading a Magento mail template and fill its vars from code?

Loading Magento Email Templates: Filling Variables Without Sending Mail

In Magento, loading an email template directly from code typically returns a string containing placeholders for variables like {{var myvar}} or {{store url=""}}. While these are necessary for sending actual mail templates, you might find yourself needing to display the content with filled placeholders to users. This post outlines how to achieve this without having to send an email.

Understanding Magento Email Templates

Before diving into the solution, let’s clarify what happens when you load a template using Mage::getModel('core/email_template'). The loaded string contains variables that are only resolved and replaced during the final email sending process. These placeholders include:

  • {{var myvar}}: Replaced with the value of variable myvar
  • {{store url=""}}: Replaced with the base URL of the current store
  • {{template_text}}: Replaced with the actual text content

However, getting the template using $mailTemplate->load($templateId) and then accessing $myTemplate->getData('template_text') won’t fill these variables. The string returned is static and contains only the placeholders.

Loading Templates for Previewing: A Workaround

To preview a Magento email template with filled variables, you can use a workaround involving the Mage_Core_Model_Email_Template class method getMail()->getBodyHtml(). This method prepares the actual HTML content of an email, resolving all placeholders during this preparation.

Here’s how to implement it:

$mailTemplate = Mage::getModel('core/email_template');
$myTemplate = $mailTemplate->load($templateId);

$preparedText = $myTemplate->getMail()->getBodyHtml();

The $preparedText variable now contains the template text with all placeholders filled according to the current context (like store URL, currency code, etc.). It’s important to note that this method assumes the template is loaded in a Magento context where the necessary variables are set (like Mage::app()->setCurrentStore($storeId)).

Displaying Filled Templates to Users

Once you have the $preparedText string with filled placeholders, you can display it on your frontend or backend interface. For example:

echo $this->helper('core')->escapeHtml($preparedText);

Or in a Magento template file:

{{var prepared_text}}

Conclusion: Previewing Email Templates

To preview Magento email templates with filled placeholders without sending actual mail, you can use the getMail()->getBodyHtml() method. This approach requires understanding and managing the context (like setting the current store).

Remember to handle potential errors or edge cases when using this method in a production environment. Always ensure that your Magento context is properly set before loading templates.

For further information on Magento 2 template handling, consider visiting Laravel Company’s Magento Development Resources for more detailed guides and tutorials on best practices and advanced features.

Themes Email

Our network