magento email:发送自定义邮件

69 篇文章 0 订阅
在magento中实现简单的邮件发送并不复杂,不过要想用特定邮件模板,就需要对magento邮件系统做一些深入了解,本文就分析一下如何发送自定义邮件。之前已经发了一篇介绍magento基本邮件设置的文章 Magento Transactional Emails常规设置,大家可以先了解一下。

有几个关键的点先说一下,大家好有个印象,system.xml,config.xml,core_config_data(table名),邮件模板(Admin->System->Transactional Emails),这几个因素在配置自定义邮件过程中几乎都会用到,但是如果对magento email机制比较了解的话,就可以省掉一些因素,快速的实现自定义邮件发送功能,当然为了加深了解,本文会提到所有因素。

首先是system.xml,这个文件建立之后,在后台System->Configuration的对应tab中,就会找到相应的配置,请看如下示例:

  1. <?xml version="1.0"?>  
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <config>  
  4.     <sections>  
  5.         <customer translate="label" module="employee">  
  6.             <groups>  
  7.                 <quote_email translate="label">  
  8.                     <label>Quote Emails</label>  
  9.                     <frontend_type>text</frontend_type>  
  10.                     <sort_order>5</sort_order>  
  11.                     <show_in_default>1</show_in_default>  
  12.                     <show_in_website>0</show_in_website>  
  13.                     <show_in_store>0</show_in_store>  
  14.                     <fields>  
  15.                             <exist_user_quote_template translate="label">  
  16.                                 <label>Existing User Quote Email</label>  
  17.                                 <frontend_type>select</frontend_type>  
  18.                                 <source_model>adminhtml/system_config_source_email_template</source_model>  
  19.                                 <sort_order>3</sort_order>  
  20.                                 <show_in_default>1</show_in_default>  
  21.                                 <show_in_website>1</show_in_website>  
  22.                                 <show_in_store>1</show_in_store>  
  23.                             </exist_user_quote_template>  
  24.                     </fields>  
  25.                 </quote_email>  
  26.             </groups>  
  27.         </customer>  
  28.     </sections>  
  29. </config>  
xml中的sections标签紧跟的是customer标签,所以进入System->Configuration后,在左侧要点击Customer Configuration(因为标签customer已经给出了限定范围)标签,找到Quote Emails,会看到有一个Existing User Quote Email的label,这些都是在system.xml里配置的,当然如果想要改变这些label,只需要在system.xml中改就可以。

这里要注意的是<source_model>adminhtml/system_config_source_email_template</source_model>这个决定了Existing User Quote Email对应的下拉框,包含了所有的已存在的邮件模板,并且在(Admin->System->Transactional Emails)中可以看到并修改他们。

接下来看config.xml文件

  1. <template>  
  2.            <email>  
  3.                <customer_quote_email_exist_user_quote_template translate="label" module="employee">  
  4.                    <label>jonas Emails</label>  
  5.                    <file>quote/exist_user.html</file>  
  6.                    <type>html</type>  
  7.                </customer_quote_email_exist_user_quote_template>  
  8.            </email>  
  9.        </template>  
注意标签<customer_quote_email_exist_user_quote_template>,它和system.xml里的标签是有关联的,刚好是system.xml里的<customer>,<quote_email>,<exist_user_quote_template>这三个标签的组合,并且这里一定要这么写,不这么写的话,这个模板文件exist_user.html在Customer Configuration的下拉选项中就不会出现。然后别忘了在app/locale/en_US/template/email/quote/下面要添加exist_user.html;注意这里(config.xml)的label的值是jonas Emails,进入System->Configuration,在左侧点击Customer Configuration标签,在Quote Emails下面,对应Existing User Quote Email的下拉框就会看到名称为jonas Emails的选项(前面说了,如果config和system的标签关联不上的话,这里就不会出现jonas Emails),选择他之后,一定要点击右上角的save config按钮,这个设置才能被保存到DB,即table core_config_data,注意在DB中保存的值并不是jonas Emails,而是它的父标签customer_quote_email_exist_user_quote_template;
如下图:

当上述步骤都完成后,再看code如何实现发邮件,一个简单例子:

  1.                      define('EMAIL_TEMPLATE'"customer/quote_email/exist_user_quote_template");  
  2. $mailSubject = 'my subject';  
  3. $sender = Array('name'  => 'Customer Service',  
  4.             'email' => 'mail@test.com');  
  5. $to = array('service@test.com');  
  6.   
  7. /*This is optional*/  
  8. $storeId = Mage::app()->getStore()->getId();   
  9. $template = Mage::getStoreConfig(EMAIL_TEMPLATE);   
  10. $mailConfirm = Mage::getModel('core/email_template');  
  11. $translate  = Mage::getSingleton('core/translate');  
  12.   
  13. $mailConfirm ->setTemplateSubject($mailSubject)  
  14.          ->sendTransactional($template$sender$to'',   
  15.                                   Array('subject'=>$mailSubject,'customer'=>$customer),$storeId);  
  16. $translate->setTranslateInline(true);      
例子中的EMAIL_TEMPLATE是customer/quote_email/exist_user_quote_template,实际上就是system.xml中三个标签的组合。当调用函数Mage::getStoreConfig(EMAIL_TEMPLATE) 的时候,magento会去查找core_config_data这个表,找到customer/quote_email/exist_user_quote_template对应的的值是什么(如果这个值在DB不存在,会转向config.xml查找,这是另外一种情况,下次介绍)。这里对应的就是customer_quote_email_exist_user_quote_template,然后magento才会去config.xml匹配这个值,最终找到模板quote/exist_user.html。

最后解释下core_config_data这个table如下图所示:


可以看到customer/quote_email/exist_user_quote_template对应的value是customer_quote_email_exist_user_quote_template,前面已经介绍Mage::getStoreConfig(EMAIL_TEMPLATE)实质上就是Mage::getStoreConfig('customer_quote_email_exist_user_quote_template'),而在config中我们已经定义
customer_quote_email_exist_user_quote_template对应的模板是exist_user.html,所以这里实际上使用的模板就是exist_user.html。或许你已经发现有些path对应的value是个数字,这里有个26,其实26是邮件模板的ID,点击Admin->System->Transactional Emails,就会看到每个模板的ID;就是说,本文介绍的是较复杂的magento发送自定义邮件的方法,还有一些其他方法也能实现发送自定义邮件,比如用已存在email template来修改,无需再添加config.xml和system.xml,这样在core_config_data中的value就会是一个数字,发送邮件的时候,使用的就是ID为26所对应的模板;更多的时候,我们都不会再去重新写html,config等文档,只需要在Admin->System->Transactional Emails新加一个template,然后使用这个template即可,还有一种稍简单点的不需添加system.xml的的自定义邮件发送方法magento email:快速实现发送自定义邮件


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值