I have created an email template for a custom module, placed the file in app/locale/en_US/template/email
and configured it in my module config XML file. Now I want to retrieve that template in a controller through code. I have tried :
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('custom_template');
But it returns a NULL
email template. My module email template config is :
<global>
<template>
<email>
<custom_template>
<label>Some custom email template</label>
<file>custom_template.html</file>
<type>html</type>
</custom_template>
</email>
</template>
</global>
What am I missing?
** Edit **
I have found this code, but the line
$template_collection = Mage::getResourceSingleton('core/email_template_collection');
returns an empty collection. I tried to look into into Magento's admin source and found Mage_Adminhtml_Block_System_Email_Template_Grid
which also uses the same line to get a collection and, apparently, it works for Magento, but not with my code. Why?
The PHP you posted
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('custom_template');
will load an email template from the database. Specifically, from the core_email_template
table. The template you've placed on the file system is the default template. You should be able to load it using the loadDefault
method.
$emailTemplate = Mage::getModel('core/email_template')->loadDefault('custom_template');
If anyone is looking for full sample code of how to send a Magento email based on an existing Magento email template, the following works well. It does not require any XML config.
// This is the name that you gave to the template in System -> Transactional Emails
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('My Custom Email Template');
// These variables can be used in the template file by doing {{ var some_custom_variable }}
$emailTemplateVariables = array(
'some_custom_variable' => 'Hello World'
);
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
$emailTemplate->setSenderName('Joe Bloggs');
$emailTemplate->setSenderEmail('[email protected]');
$emailTemplate->setTemplateSubject("Here is your subject");
$emailTemplate->send('[email protected]', 'Joanna Bloggs', $emailTemplateVariables);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With