这一节我们来看下如何在自己的module中配置和使用邮件模板
1、首先我们要建立自己的Module(怎么建module不在这节的讨论范围内)Giftcards
2、在module的config.xml中添加email的全局配置
<global>
<email>
<giftcards_email_email_template translate="label" module="giftcards"><!--理论上不一定得用giftcards_email_email_template只要保证全局唯一就可,但为了跟system.xml配合,推荐这么命名-->
<label>Gift Card E-mail</label><!--这个label会在后台配置的时候显示出来,也与system.xml有关--> <file>giftcard_email.html</file><!--locale邮件模板的名字--> <type>html</type><!--邮件内容类型,是text还是html,这个会在发邮件的时候用到--> </giftcards_email_email_template> </email></global>
<?xml version="1.0"?>
<config>
<tabs>
<webtex translate="label" module="giftcards">
<label>WebTex</label>
<sort_order>300</sort_order>
</webtex>
</tabs>
<sections>
<giftcards module="giftcards">
<tab>webtex</tab>
<label>Gift Cards</label>
<frontend_type>text</frontend_type>
<sort_order>200</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<email module="giftcards">
<label>Email Options</label>
<frontend_type>text</frontend_type>
<sort_order>50</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<email_template>
<label>Template for e-mail</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</email_template>
</fields>
</email>
</groups>
</giftcards>
</sections>
</config>我们来看source_model的源码Mage_Adminhtml_Model_System_Config_Source_Email_Template
class Mage_Adminhtml_Model_System_Config_Source_Email_Template extends Varien_Object
{
const XML_PATH_TEMPLATE_EMAIL = 'global/template/email/';
public function toOptionArray()
{
if(!$collection = Mage::registry('config_system_email_template')) {
$collection = Mage::getResourceModel('core/email_template_collection')
->load();
Mage::register('config_system_email_template', $collection);
}
$options = $collection->toOptionArray();
$templateName = Mage::helper('adminhtml')->__('Default Template from Locale');
$nodeName = str_replace('/', '_', $this->getPath());//获取当前system.xml中的path
$templateLabelNode = Mage::app()->getConfig()->getNode(self::XML_PATH_TEMPLATE_EMAIL . $nodeName . '/label');//根据path的值对应config.xml中的node,然后获取对应的Label,所以要保证path于config.xml中的node名一致
if ($templateLabelNode) {
$templateName = Mage::helper('adminhtml')->__((string)$templateLabelNode);
$templateName = Mage::helper('adminhtml')->__('%s (Default Template from Locale)', $templateName);
}
array_unshift(
$options,
array(
'value'=> $nodeName,
'label' => $templateName
)
);
return $options;
}
}这个类的作用是为后台的giftcards/email/email_template form表单select提供选项,这里的选项由两部分组成,一部分是从core_email_template表中获取的邮件模板(代码红色部分),另一部分是代码中的绿色部分,这部分是从system.xml和config.xml的配置中得来的
上面配置完成后会在后台看到如下的模板配置设置

在这里可以选择使用什么模板
4、根据前面的内容配置好后,邮件发送过程中模板是如何加载的呢
模板邮件发送的核心类是Mage_Core_Model_Email_Template
发邮件的基本代码如下:
$post = array(
'amount' => $amount,
'code' => $this->getCardCode(),
);
$template='giftcards/email/email_template';
$email="god_chen@sina.com";
$translate = Mage::getSingleton('core/translate');
$translate->setTranslateInline(false);
$postObject = new Varien_Object();
$postObject->setData($post);
$mailTemplate = Mage::getModel('core/email_template');
$mailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $storeId))
->sendTransactional(
Mage::getStoreConfig($template),//邮件模板设置
'general',//发件人设置
$email,//收件人
null,
array('data' => $postObject)//模板参数
);
$translate->setTranslateInline(true);Mage::getStoreConfig($template)是从后台获取templateid,这个ID会传入Mage_Core_Model_Email_Template 的sendTransactional方法,在该方法中调用模板的关键代码是:if (is_numeric($templateId)) {
$this->load($templateId);
} else {
$localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
$this->loadDefault($templateId, $localeCode);
}如果传入的templateId是数字的话就会从core_email_template中根据id获取模板,否则就会根据config.xml中node名字为giftcards_email_email_template的邮件配置获取模板文件和邮件正文类型
<template>
<email>
<giftcards_email_email_template translate="label" module="giftcards">
<label>Gift Card E-mail</label>
<file>giftcard_email.html</file>
<type>html</type>
</giftcards_email_email_template> </email> </template>然后把相关的模板参数传入模板中编译最后会返回一个字符串存入$mail中,最后用Zend_Mail来发送邮件
$mail->send($transport)
本文介绍在Magento中如何配置和使用自定义模块的邮件模板。通过修改config.xml和system.xml文件,实现邮件模板的选择与发送。文章详细展示了配置步骤及核心类Mage_Core_Model_Email_Template的工作原理。
18万+

被折叠的 条评论
为什么被折叠?



