I'm trying to add a phtml block to the sales email using the admin area to edit the template. In Magento 1.x this worked fine, however I'm not sure how to do this the same way in Magento 2.3x?

I've added {{block class='Magento\\Framework\\View\\Element\\Template' area='frontend' template='Vendor_Module::email/ordertrustpilot_afs.phtml'}}

to the template, however I get an error in the log for main.CRITICAL: Invalid template file: 'Vendor_Module::email/ordertrustpilot_afs.phtml' in module: '' block's name: 'magento\framework\view\element\template_0' [] []

Where should I place the phtml file in Magento 2.3x for this to work as intended?

Many thanks


Following Amitkumar's advice I can now output using phtml, however I now need to see why the code in the phtml does not work. I'm using the same code as I did in Magento 1.x which is now obviously wrong.

echo "START"; works at the beginning of the phtml and shows in the email content, however the rest of the code doesn't seem to execute or produce an output or an error.

The code used is below and should output various order details needed to send to Trust Pilot to automate product reviews:

<?php echo "START"; ?>
<?php $finalorderid = $this->getIncrementId() ?>
<?php $_order = $this->getOrder(); ?>
<?php if (!empty($_order)): ?>
<?php $orderId = $_order->getIncrementId(); ?>
<?php $orderRecipientName = $_order->getCustomerName(); ?>
<?php $orderRecipientEmail = $_order->getCustomerEmail(); ?>
<?php $productsData = ""; ?>
<?php $i=0; foreach ($_order->getAllItems() as $_ship): ?>
<?php $_item = Mage::getModel('catalog/product')->load($_ship->getProductId()); ?>
<?php $productsData .= ($i == 0) ? "{" : ",{" ?>
<?php $productsData .= '"productUrl": "'.Mage::getBaseUrl().$_item->getData('url_key').'.html",'; ?>
<?php $productsData .= '"imageUrl": "'.$_item->getImageUrl().'",'; ?>
<?php $productsData .= '"name": "'.$_item->getName().'",'; ?>
<?php $productsData .= '"sku": "'.$_item->getSku().'",'; ?>
<?php $productsData .= '}'; $i++;?> <?php endforeach; ?>
<?php echo '<script type="application/json+trustpilot">{'; ?>
<?php echo '"templateId": "599c4755e05df80becabb8ae",'; ?>
<?php echo '"locale": "en-GB",'; ?>
<?php echo '"recipientEmail": "'.$orderRecipientEmail.'",'; ?>
<?php echo '"recipientName": "'.$orderRecipientName.'",'; ?>
<?php echo '"referenceId": "'.$orderId.'"'; ?>
<?php if (!empty($productsData)): ?>
<?php echo ', "products": ['; ?>
<?php echo $productsData; ?>
<?php echo ']'; ?>
<?php endif; ?>
<?php echo '}'; ?>
<?php echo '</script>'; ?>
<?php endif; ?>

2 Answers 2


Please refer to this answer

Add custom block in transnational email.

Let me know if it is not working

  • Hi Amitkumar, thank you for your help. I have managed to produce an output to the email content following your advice, however the code within the phtml now does not do what I desire? Do you have any pointers. Thanks. Please see my updated question.
    – Steve
    Aug 26, 2020 at 11:35
  • Kindly upvote and accept if my answer helps.Also please share all the code so i can check. in your code i see you need to check each function call in your phtml file is exit or not so it won't break. Aug 26, 2020 at 11:43
  • The code throwing the error is now: <?php $_item = Mage::getModel('catalog/product')->load($_ship->getProductId()); ?>
    – Steve
    Aug 26, 2020 at 12:46
  • lol buddy this is the code for Magento 1. refer this link magento.stackexchange.com/questions/94851/… Aug 26, 2020 at 13:37
  • lol, yes I recognise this, but I am new to Magento 2.x. I have since used $_item = $_objectManager->create('Magento\Catalog\Model\Product')->load($_ship->getProductId()); which works, but then I do not know how to get product url and other information properly. I was using: ` <?php $productsData .= '"productUrl": "'.Mage::getBaseUrl().$_item->getData('url_key').'.html",'; ?> <?php $productsData .= '"imageUrl": "'.$_item->getImageUrl().'",'; ?> <?php $productsData .= '"name": "'.$_item->getName().'",'; ?> <?php $productsData .= '"sku": "'.$_item->getSku().'",'; ?>`
    – Steve
    Aug 26, 2020 at 13:43

You need to use this code :

{{block class='Magento\\Framework\\View\\Element\\Template' area='adminhtml' template='Vendor_Module::email/ordertrustpilot_afs.phtml'}}

And confirm, you are created file into specific location.

  • Unfortunately, this still returns the same error. The file location is vendor/magento/module-sales/view/frontend/email/ordertrustpilot_afs.phtml I think perhaps I am using the wrong location.
    – Steve
    Aug 25, 2020 at 13:36
  • check this things : area='adminhtml' Aug 25, 2020 at 14:04
  • Yes, I did change this. Thanks, it is still the same error.
    – Steve
    Aug 26, 2020 at 9:27
  • what is the phtml file location? Aug 26, 2020 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.