magento email:发送自定义邮件

来源:互联网 发布:数据库审计是干什么 编辑:程序博客网 时间:2024/05/16 13:53
邮件是几乎所有电商系统都要用到的功能,在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中,就会找到相应的配置,请看如下示例:

<?xml version="1.0"?><?xml version="1.0" encoding="UTF-8"?><config>    <sections>        <customer translate="label" module="employee">            <groups>                <quote_email translate="label">                    <label>Quote Emails</label>                    <frontend_type>text</frontend_type>                    <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_user_quote_template translate="label">                                <label>Existing User Quote Email</label>                                <frontend_type>select</frontend_type>                                <source_model>adminhtml/system_config_source_email_template</source_model>                                <sort_order>3</sort_order>                                <show_in_default>1</show_in_default>                                <show_in_website>1</show_in_website>                                <show_in_store>1</show_in_store>                            </exist_user_quote_template>                    </fields>                </quote_email>            </groups>        </customer>    </sections></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文件

<template>            <email>                <customer_quote_email_exist_user_quote_template translate="label" module="employee">                    <label>jonas Emails</label>                    <file>quote/exist_user.html</file>                    <type>html</type>                </customer_quote_email_exist_user_quote_template>            </email>        </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如何实现发邮件,一个简单例子:

                        define('EMAIL_TEMPLATE', "customer/quote_email/exist_user_quote_template");$mailSubject = 'my subject';$sender = Array('name'  => 'Customer Service',        'email' => 'mail@test.com');$to = array('service@test.com');/*This is optional*/$storeId = Mage::app()->getStore()->getId(); $template = Mage::getStoreConfig(EMAIL_TEMPLATE); $mailConfirm = Mage::getModel('core/email_template');$translate  = Mage::getSingleton('core/translate');$mailConfirm ->setTemplateSubject($mailSubject)     ->sendTransactional($template, $sender, $to, '',                                      Array('subject'=>$mailSubject,'customer'=>$customer),$storeId);$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:快速实现发送自定义邮件

本文链接:http://blog.csdn.net/shangxiaoxue/article/details/7759591

原创粉丝点击