Magento – how to call phtml file in email template in magento2

emailemail-templatesmagento2magento2.3phtml

I need to pass the phtml file into email template. but I'm getting error like

Error filtering template: Invalid template file: 'Vendor_Module::emailproducts.phtml' in module: '' block's name: 'magento\framework\view\element\template_0'

I used below code in email template.

{{block class="Magento\Framework\View\Element\Template" name="email_template" area='frontend' template="Vendor_Module::emailproducts.phtml"}}

Thanks in advance.

Best Answer

Steps to call PHTML file in custom email template:

  1. Write below code in email template to call custom phtml file.

    You can pass custom data in $custom_var to PHTML file.

{{layout handle="custom_email_data" custom_var=$custom_var area="frontend"}}
  1. Create layout file at below path :

Vendor/Module/view/frontend/layout/custom_email_data.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Custom email data" >
    <body>
        <block class="Magento\Framework\View\Element\Template" name="custom_email_data" template="Vendor_Module::email/custom.phtml"/>
    </body>
</page>
  1. Create PHTML file at below path :

Vendor/Module/view/frontend/templates/email/custom.phtml

<?php 

/*** Get custom variable data ***/
 echo $block->getCustomVar(); 

?>
Related Topic