用Magento的Email模板机制发邮件

Magento的Mage::getModel('core/email_template')模型可用来发信。

[b]步骤I.[/b]
在你的模块(其实任意Module都可以)的etc/config.xml的根标签<config>下添加代码

<default>
<{限定名1}>
<{限定名n}>
<enabled>1</enabled>
<template>{email模板标签名}</template>
</{限定名n}>
</限定名>
</default>
<global>
<template>
<email>
<{{email模板标签名} translate="label" module="{模块名}">
<label>{任何标识性名}</label>
<file>{email模板html文件名}</file>
<type>html</type>
</{email模板标签名>
</email>
</template>
</global>

a).在global/template/email下定义新的email模板标签,模板文件名。模板文件名类似CMS页,要放到app/locale/{当前语种}/template/email目录或子目录下
b). default/下限定名1...限定名N围绕的template引用global下定义的email模板
限定名1...限定名N表示标签可以嵌套一层或多层用于与其他default下标签区分。
一个例如下:

<default>
<customer_email>
<services_request>
<enabled>1</enabled>
<template>customer_email_service_template</template>
</services_request>
</customer_email>
</default>
<global>
<template>
<email>
<customer_email_service_template translate="label" module="sales">
<label>Customer Services Request</label>
<file>customer_services.html</file>
<type>html</type>
</customer_email_service_template>
</email>
</template>
</global>

[b]步骤II.[/b]
创建customer_services.html放到app/locale/{当前语种}/template/email目录下(内容省略).

[b]步骤III. 代码调用例[/b]

/* @var $translate Mage_Core_Model_Translate */
$translate = Mage::getSingleton('core/translate');
$translate->setTranslateInline(false);

$storeId = Mage::app()->getStore()->getId();
$template = Mage::getStoreConfig('customer_email/services_request/template', $storeId);
$recipient = array(
'name' => 'Baby',
'email' => 'elon.guo@gmail.com'
);
$sender = array(
'name' => 'Koda Guo',
'email' => 'koda.guo@gmail.com'
);

$mailTemplate = Mage::getModel('core/email_template');
$mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$storeId))
->sendTransactional(
$template,
$sender,
$recipient['email'],
$recipient['name'],
array( // parameters to email
'param1'=> 'abc',
'param2'=> 'def',
'param3'=> 'ghi'
)
);
$translate->setTranslateInline(true);


使用Magento模板机制,一旦定义了新的模板,就可以在后台System->Transactional Mail处定制该模板,从而方便维护.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值