有几个关键的点先说一下,大家好有个印象,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>
这里要注意的是<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>
如下图:

当上述步骤都完成后,再看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);
最后解释下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:快速实现发送自定义邮件!