{"id":5856,"date":"2020-10-20T07:59:00","date_gmt":"2020-10-20T07:59:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/?p=5856"},"modified":"2025-05-22T11:36:56","modified_gmt":"2025-05-22T06:06:56","slug":"use-phtml-file-email-template-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/use-phtml-file-email-template-magento-2\/","title":{"rendered":"How to Use phtml File in Email Template in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 store owners send emails to customers for not only marketing purposes but also for sending the order, shipping, and invoice details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For branding purposes, the merchants use&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/customize-email-templates-in-magento-2\/\">customize email templates<\/a>&nbsp;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are the steps that you have to follow for checking the output of the design of the email template from the admin panel:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/10\/Email-template-grid-1024x337.png\" alt=\"Email template grid\" class=\"wp-image-11002\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Login to Magento 2 admin panel<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Visit&nbsp;<strong>Marketing &gt; Communications &gt; Email Templates&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Click on&nbsp;<strong>Add New Template<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Select and click on&nbsp;<strong>Load Template<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/10\/Email-template-preview-1024x578.png\" alt=\"Email template preview\" class=\"wp-image-11005\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Now, click on&nbsp;<strong>Preview Template&nbsp;<\/strong>to see the changes that you have made.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the programmatic method to&nbsp;<em><strong>use phtml file in Email template in Magento 2<\/strong><\/em>&nbsp;can be a better option that eliminates the above steps and removes the admin\u2019s dependency.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Use phtml File in Email Template in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/registration.php<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php \nMagento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(\n    ComponentRegistrar::MODULE, \n    'Vendor_Extension', \n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/etc\/module.xml<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;? xml version = \"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Extension\" setup_version=\"1.0.0\">\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/etc\/frontend\/routes.xml<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;? xml version = \"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\">\n    &lt;router id=\"standard\">\n        &lt;route id=\"extension\" frontName=\"extension\">\n            &lt;module name=\"Vendor_Extension\"\/>\n        &lt;\/route>\n    &lt;\/router>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/Controller\/Index\/Index.php<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php namespace Vendor\\Extension\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\nuse Magento\\Framework\\App\\Action\\Action;\n\nclass Index extends Action\n{\n    protected $resultPageFactory;\n\n    public function __construct(Context $context, PageFactory $resultPageFactory)\n    {\n        parent::__construct($context);\n        $this->resultPageFactory = $resultPageFactory;\n    }\n\n    public function execute()\n    {\n        return $resultPage = $this->resultPageFactory->create();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/view\/frontend\/layout\/extension_index_index.xml<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"1column\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceContainer name=\"content\">\n            &lt;block class=\"Vendor\\Extension\\Block\\Extension\" name=\"extension_index_index\"\n                   template=\"Vendor_Extension::template_file.phtml\"\/>\n        &lt;\/referenceContainer>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/Block\/Extension.php<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php namespace Vendor\\Extension\\Block;\n\nuse Magento\\Framework\\View\\Element\\Template;\n\nclass Extension extends Template\n{\n    protected $_template = 'Vendor_Extension::template_file.phtml';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/view\/frontend\/templates\/template_file.phtml<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">This is the content of the phtml file<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/view\/frontend\/email\/email_template.html<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!--@subject Use phtml file in email template @-->\n&lt;!--@vars {\"store url=\\\"\\\"\":\"Store Url\", \"skin url=\\\"images\/logo_email.gif\\\" _area='frontend'\":\"Email Logo Image\"} @-->\n&lt;!--@styles body,td { color:#2f2f2f; font:11px\/1.35em Verdana, Arial, Helvetica, sans-serif; } @--> {{template\nconfig_path=\"design\/email\/header_template\"}} \n{{var template|raw}}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><em><strong>app\/code\/Vendor\/Extension\/etc\/email_templates.xml<\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Email:etc\/email_templates.xsd\">\n    &lt;template id=\"my_custom_email_template\"\n              label=\"Use phtml file in email template\"\n              file=\"email_template.html\"\n              type=\"html\"\n              module=\"Vendor_Extension\"\n              area=\"frontend\"\n    \/>\n\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>app\/code\/Vendor\/Extension\/Helper\/Data.php<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Helper;\n\nuse Magento\\Framework\\App\\Area;\nuse Magento\\Framework\\Exception\\LocalizedException;\nuse Magento\\Framework\\Mail\\Template\\TransportBuilder;\nuse Magento\\Framework\\View\\Element\\AbstractBlock;\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Store\\Model\\App\\Emulation;\nuse Vendor\\Extension\\Block\\Extension;\n\nclass Data extends Template\n{\n\n    private $transportBuilder;\n\n    private $appEmulation;\n\n    public function __construct(\n        Emulation $appEmulation,\n        TransportBuilder $transportBuilder,\n        Template\\Context $context,\n        array $data = []\n    ) {\n        parent::__construct($context, $data);\n        $this->transportBuilder = $transportBuilder;\n        $this->appEmulation = $appEmulation;\n    }\n\n    \/**\n     * Create block instance\n     *\n     * @param string|AbstractBlock $block\n     * @return AbstractBlock\n     * @throws LocalizedException\n     *\/\n    public function createBlock($block)\n    {\n        if (is_string($block)) {\n            if (class_exists($block)) {\n                $block = $this->_layout->createBlock($block);\n            }\n        }\n        if (!$block instanceof AbstractBlock) {\n            throw new LocalizedException(__('Invalid block type: %1', $block));\n        }\n        return $block;\n    }\n    \/\/call this function to send mail\n    public function sendEmail()\n    {\n        \/\/ this is an example and you can change template id,fromEmail,toEmail,etc as per your need.\n        $templateId = 'my_custom_email_template'; \/\/ template id\n        $fromEmail = 'fromMail@email.com';  \/\/ sender Email id\n        $fromName = 'From Name';             \/\/ sender Name\n        $toEmail = 'tomail@email.com'; \/\/ receiver email id\n\n        try {\n\n            $block = $this->createBlock(Extension::class);\/\/pass reference of block class here, connected with phtml file\n            \/\/ make sure in your block class there is\n            \/\/ $_template variable exist which specifies the reference to the phtml file like\n            \/\/ protected $_template = 'Vendor_Module::template.phtml';\n            $template = $this->_appState->emulateAreaCode(\n                Area::AREA_FRONTEND,\n                [$block, 'toHtml']\n            ); \/\/ pass this variable as template variable of email template.\n            \/\/ template variables pass here\n            $templateVars = [\n                'template' => $template\n            ];\n            \/\/ use this template variable in email template as {{var template|raw}}\n            $storeId = $this->_storeManager->getStore()->getId();\n\n            $from = ['email' => $fromEmail, 'name' => $fromName];\n            $this->inlineTranslation->suspend();\n\n            $templateOptions = [\n                'area' => Area::AREA_FRONTEND,\n                'store' => $storeId\n            ];\n            $transport = $this->transportBuilder->setTemplateIdentifier($templateId)\n                ->setTemplateOptions($templateOptions)\n                ->setTemplateVars($templateVars)\n                ->setFrom($from)\n                ->addTo($toEmail)\n                ->getTransport();\n            $transport->sendMessage();\n            $this->inlineTranslation->resume();\n        } catch (\\Exception $e) {\n            $this->_logger->info($e->getMessage());\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do consider sharing this article with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-5856","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/5856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=5856"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/5856\/revisions"}],"predecessor-version":[{"id":14645,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/5856\/revisions\/14645"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=5856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=5856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=5856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}