Magento – Create transactional email via install/upgrade script

email-templatesmagento-enterprisesetup-scripttransactional-mail

How can I create transactional email templates via an install/upgrade script? I need them populated in Transactional Emails rather than having email templates available in the app/locale folder.

Best Answer

In a situation like this it's generally useful to follow the logic of the corresponding admin user action, which posts to Mage_Adminhtml_System_Email_TemplateController::saveAction():

$template->setTemplateSubject($request->getParam('template_subject'))
    ->setTemplateCode($request->getParam('template_code'))
    ->setTemplateText($request->getParam('template_text'))
    ->setTemplateStyles($request->getParam('template_styles'))
    ->setModifiedAt(Mage::getSingleton('core/date')->gmtDate())
    ->setOrigTemplateCode($request->getParam('orig_template_code'))
    ->setOrigTemplateVariables($request->getParam('orig_template_variables'));

if (!$template->getId()) {
    $template->setAddedAt(Mage::getSingleton('core/date')->gmtDate());
    $template->setTemplateType(Mage_Core_Model_Email_Template::TYPE_HTML);
}

if ($request->getParam('_change_type_flag')) {
    $template->setTemplateType(Mage_Core_Model_Email_Template::TYPE_TEXT);
    $template->setTemplateStyles('');
}

$template->save();

You can essentially do the same in your setup script, with the obvious difference being that instead of POST params accessed via a request object you are creating your own array directly in the code.

Related Topic