簡體   English   中英

Magento按代碼獲取電子郵件模板

[英]Magento get email template by code

我已經為自定義模塊創建了一個電子郵件模板,將該文件放在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,但不適用於我的代碼。 為什么?

你發布的PHP

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

將從數據庫加載電子郵件模板。 具體來說,來自core_email_template表。 您放在文件系統上的模板是默認模板。 您應該能夠使用loadDefault方法加載它。

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

如果有人正在尋找基於現有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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM