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

How to customize Magento 2 contact us form

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.

Frequently Asked Questions (FAQ)

help Why should I customize Magento 2 email templates?
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 What is the benefit of using a PHTML file for email templates?
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 What is {{var result.subject.value}}?
{{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 How do I create a custom module in Magento 2?
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 How do I enable my custom module in Magento 2?
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 How can I test my customized email template?
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 Can I use this method for other email templates in Magento 2?
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.