2

I need to get email template content like text/html using template id/code programmatically any help/suggestion much appreciated.

Thanks in advance.

2 Answers 2

3

Using the below helper method you can get the template data

<?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)){
                $template = $this->template->load($templateId, 'template_id');
            }else {
                $template = $this->template->load($templateId, 'template_code');
            }


        $templateText = $this->template->getTemplateText();
        return $templateText;
    }
}
?>

You can add the following code to get the custom template content. Just replace the $templateId with you email identifier name.

<?php
namespace Your\Module\Model;

use Magento\Email\Model\TemplateFactory;

class YourClass
{
    protected $templateFactory;

    public function __construct(
        TemplateFactory $templateFactory
    ) {
        $this->templateFactory = $templateFactory;
    }

    public function getCustomEmailTemplateContent($templateId)
    {
        $template = $this->templateFactory->create()->loadDefault($templateId);//Replace with your custom template name
        $templateText = $template->getTemplateText();

        return $templateText;
    }
}
3
  • Thanks @Grazitti Dev I'll try above code this works only if email templates stored in 'email_template' table so i need template text also if template created directly in modules by code not created/exist in admin template grid. Oct 12, 2023 at 5:12
  • @ChandreshChauhan Updated the answer. Please try it out. Oct 16, 2023 at 5:27
  • Thank you @Grazitti Dev its working. Oct 16, 2023 at 10:38
0

You can try below code.

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Email\Model\Template" type="YourVendor\YourModule\Model\EmailTemplateContent" />
</config>

EmailTemplateContent.php

<?php

namespace YourVendor\YourModule\Model;

use Magento\Email\Model\Template;

class EmailTemplateContent extends Template
{
    /**
     * Get email template text by template ID
     *
     * @param int $templateId
     * @return string
     */
    public function getTemplateText($templateId)
    {
        $template = $this->load($templateId);
        return $template->getTemplateText();
    }
}
1
  • Thanks for sharing @nikhil but this method works only if email templates stored in table 'email_template' i need template text also if template created directly by code not created in admin template. Oct 12, 2023 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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