I have posted the solution to load Email template by template ID in Magento 2.
You can use the below code if you want to send customers an email with a customized email template.
The solution allows to load the default or custom template using the ID and get the template text.
Method to load Email template by template ID in Magento 2:
<?php namespace [Vendor]\[Module]\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Email\Model\Template as coreTemplate; class Data extends AbstractHelper { protected $template; public function __construct( Context $context, coreTemplate $template ) { $this->template = $template; parent::__construct($context); } public function templateText($templateId) { if (is_numeric($templateId)) { $this->template->load($templateId); } else { $this->template->setForcedArea($templateId); $this->template->loadDefault($templateId); } // another method if (is_numeric($templateId)){ $template = $this->template->load($templateId, 'template_id'); }else { $template = $this->template->load($templateId, 'template_code'); } $templateText = $this->template->getTemplateText(); return $templateText; } }
Create object of Helper Class and call the function.
That’s it. You can also get store name in Magento 2 email template to enhance your brand consistency, build trust with customers and also adds professional touch to your communication.
I request the readers to share the above solution with the Magento community via social media.
Thank you.