0

I want create a new template, but not from Marketing > Email Templates i want to do it programmatically from my custom extension, the idea is the new template will be saved when Setup/InstallData.php runs, is there any way to do this?

1

You can make the InstallData.php file look like this

<?php
namespace [Vendor]\[Module]\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Email\Model\TemplateFactory;


class InstallData implements InstallDataInterface
{
    /**
     * Template factory
     *
     * @var TemplateFactory
     */
    private $templateFactory;

    /**
     * Init
     *
     * @param TemplateFactory $templateFactory
     */
    public function __construct(TemplateFactory $templateFactory)
    {
        $this->templateFactory = $templateFactory;
    }
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    {
        $template = $this->templateFactory->create();
        $template->setTemplateCode('your template name here');
        $template->setTemplateText('your template text here');
        $template->setTemplateStyles('your template custom styles here');    
        $template->setTemplateSubject('your subject here');
        $template->setOrigTemplateCode('code of the template you are replacing');
        $template->save();
    }
}

Watch out for syntax errors. I didn't test the code.

| improve this answer | |
  • where the content .html i should put? and what if i dont wan't to replace the existing template? – simple guy Feb 11 '17 at 11:50
  • you have to add the content you want to be sent via email. And this won't replace a template. Sorry. I chose my words wrong. It will be an alternative to an already existing email. – Marius Feb 11 '17 at 13:00
  • the template is saved, but when i tried to send the email, i didnt receive the email, but when i change the email template to default, i received the email – simple guy Feb 13 '17 at 3:48

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.