9

我为自定义模块创建了一个电子邮件模板,将文件放入app/locale/en_US/template/email并在我的模块配置 XML 文件中进行了配置。现在我想通过代码在控制器中检索该模板。我试过了 :

$emailTemplate  = Mage::getModel('core/email_template')->loadByCode('custom_template');

但它会返回一个NULL电子邮件模板。我的模块电子邮件模板配置是:

<global>
    <template>
        <email>
            <custom_template>
                <label>Some custom email template</label>
                <file>custom_template.html</file>
                <type>html</type>
            </custom_template>
        </email>
    </template>
</global>

我错过了什么?

**编辑**

我找到了这段代码,但是这条线

$template_collection =  Mage::getResourceSingleton('core/email_template_collection');

返回一个空集合。我试图查看 Magento 的管理源代码,发现Mage_Adminhtml_Block_System_Email_Template_Grid使用同一行来获取集合,显然,它适用于 Magento,但不适用于我的代码。为什么?

4

2 回答 2

20

您发布的 PHP

$emailTemplate  = Mage::getModel('core/email_template')->loadByCode('custom_template');

将从数据库中加载电子邮件模板。具体来说,从core_email_template表。您放置在文件系统上的模板是默认模板。您应该能够使用该loadDefault方法加载它。

$emailTemplate  = Mage::getModel('core/email_template')->loadDefault('custom_template');
于 2012-05-11T03:24:19.643 回答
5

如果有人正在寻找如何根据现有的 Magento 电子邮件模板发送 Magento 电子邮件的完整示例代码,那么下面的代码效果很好。它不需要任何 XML 配置。

// 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('test@test.com');
$emailTemplate->setTemplateSubject("Here is your subject");

$emailTemplate->send('recipient@test.com', 'Joanna Bloggs', $emailTemplateVariables);
于 2015-10-05T16:02:42.793 回答