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:

Email template grid

  1. Login to Magento 2 admin panel
  2. Visit Marketing > Communications > Email Templates 
  3. Click on Add New Template
  4. Select and click on Load Template
    Email template preview
  5. 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

  1. Create app/code/Vendor/Extension/registration.php
  2. Create app/code/Vendor/Extension/etc/module.xml
  3. Create app/code/Vendor/Extension/etc/frontend/routes.xml
  4. Create app/code/Vendor/Extension/Controller/Index/Index.php
  5. Create app/code/Vendor/Extension/view/frontend/layout/extension_index_index.xml
  6. Create app/code/Vendor/Extension/Block/Extension.php
  7. Create app/code/Vendor/Extension/view/frontend/templates/template_file.phtml

  8. Create app/code/Vendor/Extension/view/frontend/email/email_template.html
  9. Create app/code/Vendor/Extension/etc/email_templates.xml
  10. 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.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

(based on 8 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a reply:

Your email address will not be published.