Magento Programming Loading Processed Email Templates

In Magento you may need to process the email text for an email but sometimes those templates are default ones, other times they can be a customized one set in the admin settings area. A customized email template from system > transactional emails menu will set in your core_config_data a numeric value for the customized email template.  There are 2 methods that load email templates one is for if the template is numeric (customized) other for if it’s just a default one from the config.

To Properly load in either case, you can use the code below:


// large array of email variables, just one here for example
$firstname = 'Joe';
$emailTemplateVariables = array('firstname'=>$firstname);
$emailTemplate = Mage::getModel('core/email_template');
// config is the config path to the email template, here is for the new order as example
$emailTemplateId = Mage::getStoreConfig('sales_email/order/template');
if (is_numeric($emailTemplateId)) {
$emailTemplate->load($emailTemplateId);
} else {
$emailTemplate->loadDefault($emailTemplateId);
}
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);