0

I have created custom mail template and successfully send mail to the customer upon order complete.

I created new mail template from admin based on the custom one I added in code, to let the marketing team edit the mail and change style.

Now I need to get marketing team changes using the new template name but it gives error this mail template not found when I do

$transport = $transportBuilder->setTemplateIdentifier('custom_email_template');

Error: Email template 'custom_email_template' is not defined

Can I use the ID instead and how to set fall back if the template was deleted to send the template base file in my custom module?

Another query how to get the mail template preview in code as html string?

Thanks in advance.

0

I have did some research in how the backend preview templates and I wrote helper for that purpose to be used in my custom module.

I used the id for that template not the identifier and I cannot set variables before rendering.

see code below:

namespace namespace\Module\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;

class Emailpreview extends \Magento\Framework\App\Helper\AbstractHelper {

public $store;

public function __construct(Context $context, StoreManagerInterface $store){
parent::__construct($context);
$this->store = $store;  
}

public function previewSerialsMailTemplate($id = '26'){
    $storeId = $this->store->getStore()->getId();
    /** @var $template \Magento\Email\Model\Template */
    $om = \Magento\Framework\App\ObjectManager::getInstance();

    $emailFactory = $om->create('\Magento\Email\Model\TemplateFactory');
    $template = $emailFactory->create();//get_class_methods
    $template->load($id);

$postObject = new \Magento\Framework\DataObject();
$postObject->setData(['message' => 'message', 'subject' => 'subject', 'customername' => 'mgomma']);

    $template->setVars(['data' => $postObject]);
    $maliciousCode = $om->create('\Magento\Framework\Filter\Input\MaliciousCode');

    $template->setTemplateText($maliciousCode->filter($template->getTemplateText()));
    $profilerName = 'email_template_proccessing';

    \Magento\Framework\Profiler::start($profilerName);
    $template->emulateDesign($storeId);

    $appState = $om->create('\Magento\Framework\App\State');
    $templateProcessed = $appState->emulateAreaCode(
        \Magento\Email\Model\AbstractTemplate::DEFAULT_DESIGN_AREA,
        [$template, 'getProcessedTemplate']
    );

    $template->revertDesign();
    if ($template->isPlain()) {
        $templateProcessed = "<pre>" . htmlspecialchars($templateProcessed) . "</pre>";
    }

    \Magento\Framework\Profiler::stop($profilerName);
    return $templateProcessed;
}
}

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.