arrow_back_ios Blog How to Customize Magento 2 Email Templates Using PHTML for Dynamic Content

How to Customize Magento 2 Email Templates Using PHTML for Dynamic Content

calendar_today July 11, 2024 visibility 2290 views folder_open Development label WebForms, Form Builder, Customization person By Vladimir Popov edit Updated July 22, 2024

MageMe WebForms lets you customize Magento 2 email notification templates using PHTML files instead of the limited admin email editor. PHTML templates give you full access to PHP logic, Magento block methods, and dynamic data — enabling conditional content, formatted tables, and file attachment links.

Customizing email templates in Magento 2 can enhance your store's communication by making emails more dynamic and tailored. This article will guide you through the process of customizing a Magento 2 email template to use a PHTML file to generate dynamic content, specifically using the variable {{var result.subject.value}}.

Introduction

In Magento 2, email templates can be customized to include dynamic content using variables. Sometimes, the built-in templating features may not suffice, and you may need to utilize a PHTML file for more complex logic. This guide will show you how to achieve this with a practical example using the {{var result.subject.value}} variable.

We will demonstrate this customization for a custom form notification created with MageMe WebForms, a Magento 2 form builder. The {{var result.subject.value}} variable is a code for the field with the Code attribute "subject."

Step-by-Step Guide to Customize Magento 2 Email Templates

1. Create a Custom Module

First, create a custom module to manage your customizations. For example, let's create a module named Vendor_CustomEmail.

app/code/Vendor/CustomEmail/
├── etc
│   └── module.xml
├── registration.php
└── view
    └── frontend
        └── templates
            └── email
                └── custom_subject.phtml
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_CustomEmail',
    __DIR__
);
?>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_CustomEmail" setup_version="1.0.0" />
</config>

2. Enable the Custom Module

Run the following commands to enable your new custom module:

php bin/magento module:enable Vendor_CustomEmail
php bin/magento setup:upgrade

3. Create the PHTML Template

Create the custom_subject.phtml file that will render the dynamic content.

<?php
$subject = strtolower((string)$block->getData('subject'));

switch ($subject) {
    case 'general question':
        echo '<p>Thank you for reaching out with your general question. We will get back to you shortly.</p>';
        break;
    case 'technical support':
        echo '<p>We have received your technical support request. Our support team will contact you soon.</p>';
        break;
    case 'pre-sale question':
        echo '<p>Thank you for your interest in our products! Our sales team will respond to your query shortly.</p>';
        break;
    default:
        echo '<p>Thank you for contacting us. We will respond to your inquiry as soon as possible.</p>';
        break;
}
?>

4. Modify the Email Template

Edit the email template to include the custom PHTML file and pass the specific variable directly. For instance, let's modify the template used by MageMe WebForms for form notifications.

<!-- Other template content -->
{{block class="Magento\Framework\View\Element\Template" area="frontend" template="Vendor_CustomEmail::email/custom_subject.phtml" subject=$result.subject.value}}
<!-- Other template content -->

5. Deploy and Test

After creating the custom module and modifying the email template, deploy the static content and clear the cache:

php bin/magento setup:static-content:deploy
php bin/magento cache:clean

Finally, test the email functionality to ensure that the dynamic content renders correctly.

Conclusion

By following these steps, you can customize Magento 2 email templates to use PHTML files for generating dynamic content based on template variables. This allows for more flexible and complex email customizations, enhancing your store's communication capabilities.

Key Takeaways

  • PHTML over admin editor — full PHP and Magento block access for email customization.
  • Dynamic data insertion — access form data, product info, and store variables in templates.
  • Conditional content — show/hide email sections based on field values.
  • File attachment handling — include download links for uploaded files in emails.
  • Reusable templates — modular PHTML templates shared across multiple forms.

Frequently Asked Questions (FAQ)

help_outline Why should I customize Magento 2 email templates? expand_more

Customizing email templates allows you to create a more personalized and professional communication channel with your customers. This can improve customer satisfaction, enhance brand image, and provide more relevant information tailored to your customers' needs.

help_outline What is the benefit of using a PHTML file for email templates? expand_more

Using a PHTML file for email templates in Magento 2 offers greater flexibility and control over the email content. It allows you to implement complex logic and dynamic content that cannot be easily achieved with the default template variables.

help_outline What is {{var result.subject.value}}? expand_more

{{var result.subject.value}} is a placeholder for a dynamic variable used in Magento 2 email templates. In this example, it refers to the "subject" field in a custom form created with MageMe WebForms, a Magento 2 form builder.

help_outline How do I create a custom module in Magento 2? expand_more

Creating a custom module in Magento 2 involves setting up the necessary file structure, including the registration.php and module.xml files. This module serves as the foundation for your customizations.

help_outline How do I enable my custom module in Magento 2? expand_more

You can enable your custom module by running the following commands:
php bin/magento module:enable Vendor_CustomEmail
php bin/magento setup:upgrade

This will register and activate your module in Magento 2.

help_outline How can I test my customized email template? expand_more

After deploying the static content and clearing the cache, trigger the email event in your Magento 2 store (e.g., submitting a form or placing an order). Check the received email to ensure that the dynamic content renders correctly as per your PHTML template.

help_outline Can I use this method for other email templates in Magento 2? expand_more

Yes, you can use this method to customize any email template in Magento 2. Simply modify the relevant email template to include your custom PHTML file and pass the necessary variables.

Using WebForms? Share Your Experience!

Vladimir Popov
About the Author
Vladimir Popov
verified Founder & Lead Developer, MageMe

Vladimir Popov is the founder and lead developer of MageMe (ACTEK d.o.o., Ljubljana, Slovenia). He has been building on Magento since 2011, starting with Magento 1 and moving to Magento 2 at its 2.0 beta. He wrote the first versions of every MageMe extension himself and still reviews every release.

His focus is clean, performance-first PHP code that plays nicely with Hyvä, Breeze, and stock Luma themes. He writes most of the technical content on the MageMe blog and answers support tickets personally for complex issues.