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.

1 Answer 1


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){
$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

$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');

    $profilerName = 'email_template_proccessing';


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

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

    return $templateProcessed;

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.