邮件是几乎所有电商系统都要用到的功能,在magento中实现简单的邮件发送并不复杂,不过要想用特定邮件模板,就需要对magento邮件系统做一些深入了解,本文就分析一下如何完成一个发送自定义邮件的模块
有几个关键的点先说一下,大家好有个印象,system.xml,config.xml,core_config_data(table名),邮件模板(Admin->System->Transactional Emails),这几个因素在配置自定义邮件过程中几乎都会用到,但是如果对magento email机制比较了解的话,就可以省掉一些因素,快速的实现自定义邮件发送功能,当然为了加深了解,本文会提到所有因素。
首先是system.xml,
app\code\local\Aps\Weicot\etc\system.xml
你也可以写在其他模块的 system.xml 中 比如 customer
/* *Weicot 兔子自定义付款系统 *付款链接发送模块 *作者 ajiang-兔子 *20151014 *1050653098@qq.com */
<config>
<tabs>
<weicotconfig translate="label" module="weicot">
<label>Weicot Config</label>
<sort_order>99999</sort_order>
</weicotconfig>
</tabs>
<sections>
<!--核心开始-->
<weicot_options translate="label" module="weicot">
<label>Weicot Email Options</label>
<tab>weicotconfig</tab>
<fontend_type>text</fontend_type>
<sort_srder>1000</sort_srder>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<weicot_email translate="label">
<label>Weicot Pay Email</label>
<frontend>text</frontend>
<sort_order>5</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<exist_pay_weicot_template translate="label">
<label>Success Pay Email</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<srot_order>3</srot_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</exist_pay_weicot_template>
<!--我写了两个模块 如果你只写一个的话 这段你可以不要-->
<fail_pays_weicot_template translate="label">
<label>Fail Pay Mail</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<srot_order>1</srot_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</fail_pays_weicot_template>
<!----------------------weicot end-------------------------->
</fields>
</weicot_email>
</groups>
</weicot_options>
<!--结束-->
</sections>
</config>
插件完整版 system.xml system
这个文件建立之后,在后台System->Configuration的对应tab中,就会找到相应的配置,请看如下示例:
xml中的sections标签紧跟的是customer标签,所以进入System->Configuration后,在左侧要点击Weicot Email Options (因为标签weicot_options已经给出了限定范围)标签,找到Weicot Pay Email,会看到有一个Success Pay Email<的label,这些都是在system.xml里配置的,当然如果想要改变这些label,只需要在system.xml中改就可以。
这里要注意的是
<source_model>adminhtml/system_config_source_email_template</source_model>
这个决定了Weicot Pay Email对应的下拉框,包含了所有的已存在的邮件模板,并且在(Admin->System->Transactional Emails)中可以看到并修改他们。
接下来看config.xml文件
/* *Weicot 兔子自定义付款系统 *付款链接发送模块 *作者 ajiang-兔子 *20151014 *1050653098@qq.com */
<!--核心开始-->
<template>
<email>
<weicot_options_weicot_email_exist_pay_weicot_template translate="label" model="weicot">
<label>Weicot Success Pay</label>
<file>weicot/exist_pay.html</file>
<type>html</type>
</weicot_options_weicot_email_exist_pay_weicot_template>
</email>
<email>
<!--我写了两个模块 如果你只写一个的话 这段你可以不要-->
<weicot_options_weicot_email_fail_pay_weicot_template translate="label" model="weicot">
<label>Weicot Fail Pay</label>
<file>weicot/fail_pay.html</file>
<type>html</type>
</weicot_options_weicot_email_fail_pay_weicot_template>
<!----------------------weicot end-------------------------->
</email>
</template>
<!--核心结束-->
插件完整版 config.xml
注意标签
weicot_options_weicot_email_exist_pay_weicot_template,
它和system.xml里的标签是有关联的,刚好是system.xml里的
< weicot_options >,< weicot_email >,< exist_pay_weicot_template >
这三个标签的组合,并且这里一定要这么写,不这么写的话,这个模板文件exist_pay.html在Weicot Email Options的下拉选项中就不会出现。然后别忘了在
app/locale/en_US/template/email/ weicot/
下面要添加exist_pay.html;注意这里(config.xml)的label的值是Weicot Success Pay,进入System->Configuration,在左侧点击Customer Configuration标签,在Weicot Email Options下面,对应Weicot Pay Email的下拉框就会看到名称为Weicot Success Pay的选项(前面说了,如果config和system的标签关联不上的话,这里就不会出现Weicot Success Pay),选择他之后,一定要点击右上角的save config按钮,这个设置才能被保存到DB,即
table core_config_data
,注意在DB中保存的值并不是Weicot Success Pay,而是它的父标签
weicot_options_weicot_email_exist_pay_weicot_template
;
发送代码例子
/*
*Weicot 兔子自定义付款系统
*付款链接发送模块
*作者 ajiang-兔子
*20151014
*1050653098@qq.com
*/
public function paySuccessMail($order){
$info=$this->getInfo($order);
$Emial=$info->getData('customer_email');
define('EMAIL_TEMPLATE',"weicot_options_weicot_email_exist_pay_weicot_template");
$mailSubject = 'Success Mail';
$email = Mage::getStoreConfig('trans_email/ident_general/email');
$name = Mage::getStoreConfig('trans_email/ident_general/name');
$sender = Array('name' => $name,
'email' => $email);
$to = array($Emial);
$storeId = Mage::app()->getStore()->getId();
$mailConfirm = Mage::getModel('core/email_template');
$template=$mailConfirm->loadDefault(EMAIL_TEMPLATE);
$translate = Mage::getSingleton('core/translate');
$sen= $mailConfirm ->setTemplateSubject($mailSubject)
->sendTransactional($template, $sender, $to, '',
$this->templateInfo($order),$storeId);
$translate->setTranslateInline(true);
}
关于在邮件模板中获得参数
/*
*Weicot 兔子自定义付款系统
*付款链接发送模块
*作者 ajiang-兔子
*20151014
*1050653098@qq.com
*/
$this->templateInfo($order);
//函数原型
public function templateInfo($order){
$info=$this->getInfo($order);
$storeId = Mage::app()->getStore()->getId();
$billingAddress=$info->getBillingAddress();
………………………………………………………………………….
………………………………………………………………………………..
$outinfo=array(
'orderId'=>$order,
'names'=>$Name,
'Phone'=>$Phone,
'company'=>$company,
'Address'=>$Address,
'City'=>$City,
'State'=>$State,
'Country'=>$Country,
'payInfo'=>$payInfo,
'status'=>$status,
'state'=>$state,
'track'=>$track,
'createdtime'=>$created_at
);
return $outinfo;
//太多了 怕页面容不下 就不写了
//可以看到这个函数返回的是数组
//而如何获得这些数据那
//像这样子就行 在邮件模板中 {{var state}} 这种方式获得数
}
当然你也可以在 邮件模板中引入其他模板就像这样
这还的多谢一个朋友的提醒
写个邮件模板 加一个自定义的lay handler
用layout handler 加block
邮件模板里 {{layout handler=xxxxx}}]
你可以 把这个发送函数 写在 controllers 里
也可以写在Model 里
当然如果你写在 Model 里可以以这种方式调用
/*
*Weicot 兔子自定义付款系统
*付款链接发送模块
*作者 ajiang-兔子
*20151014
*1050653098@qq.com
*/
public function sendMMAction(){
$mail=Mage::getModel('weicot/sendMail');
$order='100000013';
var_dump($mail->paySuccessMail($order));
}
到哪里都可以以这种方式调用
/*
*Weicot 兔子自定义付款系统
*付款链接发送模块
*作者 ajiang-兔子
*20151014
*1050653098@qq.com
*/
Mage::getModel('weicot/sendMail')->paySuccessMail($order);
//或
$mail=Mage::getModel('weicot/sendMail');
$mail->paySuccessMail($order);
简易参考资料
magento 发送邮件 简易版搞笑版