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.
2 Answers
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.
To have a clear answer, here is the installation code I usually use to create a transactional email :
<?php
$installer = $this;
$installer->startSetup();
$code = 'My Mail Title';
$subject = '{{var store.getFrontendName()}}- Mail subject';
$text = 'Content of my mail in HTML format';
$styles = 'Specific style for the mail';
$template = Mage::getModel('adminhtml/email_template');
$template->setTemplateSubject($subject)
->setTemplateCode($code)
->setTemplateText($text)
->setTemplateStyles($styles)
->setModifiedAt(Mage::getSingleton('core/date')->gmtDate())
->setAddedAt(Mage::getSingleton('core/date')->gmtDate())
->setTemplateType(Mage_Core_Model_Email_Template::TYPE_HTML);
$template->save();
$installer->endSetup();