Magento 创建发送邮件模块 完整版

7天成为Magento系统架构师,现在开始学习Magento全栈开发!

《Magento2.X企业级开发实战》

邮件是几乎所有电商系统都要用到的功能,在magento中实现简单的邮件发送并不复杂,不过要想用特定邮件模板,就需要对magento邮件系统做一些深入了解,本文就分析一下如何完成一个发送自定义邮件的模块
有几个关键的点先说一下,大家好有个印象,system.xml,config.xml,core_config_data(table名),邮件模板(Admin->System->Transactional Emails),这几个因素在配置自定义邮件过程中几乎都会用到,但是如果对magento email机制比较了解的话,就可以省掉一些因素,快速的实现自定义邮件发送功能,当然为了加深了解,本文会提到所有因素。
首先是system.xml,

appcodelocalApsWeicotetcsystem.xml

你也可以写在其他模块的 system.xml 中 比如 customer

/*
*Weicot 兔子自定义付款系统
*付款链接发送模块
*作者 ajiang-兔子 
*20151014
*1050653098@qq.com
*/

            99999
            weicotconfigtext1000111
                   text5100
                           selectadminhtml/system_config_source_email_template3111
                           selectadminhtml/system_config_source_email_template1111

插件完整版 system.xml system
这个文件建立之后,在后台System->Configuration的对应tab中,就会找到相应的配置,请看如下示例:
xml中的sections标签紧跟的是customer标签,所以进入System->Configuration后,在左侧要点击Weicot Email Options (因为标签weicot_options已经给出了限定范围)标签,找到Weicot Pay Email,会看到有一个Success Pay Emailadminhtml/system_config_source_email_template

这个决定了Weicot Pay Email对应的下拉框,包含了所有的已存在的邮件模板,并且在(Admin->System->Transactional Emails)中可以看到并修改他们。
接下来看config.xml文件

/*
*Weicot 兔子自定义付款系统
*付款链接发送模块
*作者 ajiang-兔子 
*20151014
*1050653098@qq.com
*/
       

插件完整版 config.xml
注意标签

weicot_options_weicot_email_exist_pay_weicot_template,

它和system.xml里的标签是有关联的,刚好是system.xml里的

,,

这三个标签的组合,并且这里一定要这么写,不这么写的话,这个模板文件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 发送邮件 简易版搞笑版

转载请注明:(●–●) Hello.My Weicot » Magento 创建发送邮件模块 完整版

文章来源于互联网:Magento 创建发送邮件模块 完整版

如无特殊说明或标注,任何个人或组织,复制、转载、采集本站内容请注明:
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。