1. 程式人生 > >Magento Transactional Emails常規設定 magento email:快速實現傳送自定義郵件

Magento Transactional Emails常規設定 magento email:快速實現傳送自定義郵件

郵件是幾乎所有電商系統都要用到的功能,在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' => '[email protected]');
			$to = array('[email protected]');

			/*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