How to Use phtml File in Email Template in Magento 2

Magento 2 store owners send emails to customers for not only marketing purposes but also for sending the order, shipping, and invoice details.

For branding purposes, the merchants use custom email templates over the default one. When a customer receives emails having the store name and logo with a well-organized email template design, it makes a great impression.

To check the email template design while creating, Magento 2 offers the preview option. That shows how the email template design will look in the frontend. However, the process is complicated and time-consuming as well.

Here are the steps that you have to follow for checking the output of the design of the email template from the admin panel:

  1. Login to Magento 2 admin panel
  2. Login to Magento 2 admin panel
  3. Visit Marketing > Communications > Email Templates 
  4. Click on Add New Template
  5. Select and click on Load Template
  • Now, click on Preview Template to see the changes that you have made.

However, the programmatic method to use phtml file in Email template in Magento 2 can be a better option that eliminates the above steps and removes the admin’s dependency.

Steps to Use phtml File in Email Template in Magento 2

Create app/code/Vendor/Extension/registration.php

Create app/code/Vendor/Extension/etc/module.xml

Create app/code/Vendor/Extension/etc/frontend/routes.xml

Create app/code/Vendor/Extension/Controller/Index/Index.php

Create app/code/Vendor/Extension/view/frontend/layout/extension_index_index.xml

Create app/code/Vendor/Extension/Block/Extension.php

Create app/code/Vendor/Extension/view/frontend/templates/template_file.phtml

Create app/code/Vendor/Extension/view/frontend/email/email_template.html

Create app/code/Vendor/Extension/etc/email_templates.xml

Create app/code/Vendor/Extension/Helper/Data.php

That’s it.

If you have queries regarding this post, feel free to ask in the Comments section below.

I would be happy to help you.

Do consider sharing this article with Magento Community via social media.

Thank you.

Default image
Sagar Doshi
CEO / Managing Partner

An expert in his field, Sagar Doshi is the CEO and team leader in

meghrajtechnosoft.

His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover."

Articles: 26

Leave a Reply