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' => '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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Magento 2中发送多个附件的电子邮件,您需要对`Magento\Framework\Mail\Template\TransportBuilder`类进行扩展。 下面是一个示例代码,它可以让您在Magento 2中发送多个附件的电子邮件: 1. 创建 `Vendor\Module\Model\Mail\Template\TransportBuilder.php` 文件并添加以下代码: ```php <?php namespace Vendor\Module\Model\Mail\Template; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Exception\MailException; use Magento\Framework\Mail\Template\TransportBuilder as MagentoTransportBuilder; use Magento\Framework\Mail\TransportInterfaceFactory; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Store\Model\StoreManagerInterface; class TransportBuilder extends MagentoTransportBuilder { /** * @var array */ protected $attachments = []; /** * @param array $attachments * @return $this */ public function addMultipleAttachment($attachments = []) { foreach ($attachments as $attachment) { if (file_exists($attachment['path'])) { $this->attachments[] = [ 'type' => $attachment['type'], 'name' => $attachment['name'], 'path' => $attachment['path'] ]; } } return $this; } /** * @param null|string|array $to * @param array $templateVars * @param null|string $templateOptions * @param null|string $transportOptions * * @throws MailException * * @return TransportInterfaceFactory */ public function getTransport( $to = null, array $templateVars = [], $templateOptions = null, $transportOptions = null ) { if (!empty($this->attachments)) { foreach ($this->attachments as $attachment) { $this->message->createAttachment( file_get_contents($attachment['path']), $attachment['type'], \Zend_Mime::DISPOSITION_ATTACHMENT, \Zend_Mime::ENCODING_BASE64, $attachment['name'] ); } } return parent::getTransport($to, $templateVars, $templateOptions, $transportOptions); } } ``` 2. 创建 `Vendor_Module` 模块的 `di.xml` 文件并添加以下代码: ```xml <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\Mail\Template\TransportBuilder" type="Vendor\Module\Model\Mail\Template\TransportBuilder" /> </config> ``` 3. 在您的模块中使用以下代码发送多个附件的电子邮件: ```php <?php namespace Vendor\Module\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Store\Model\StoreManagerInterface; class SendEmail extends Action { /** * @var TransportBuilder */ protected $transportBuilder; /** * @var StateInterface */ protected $inlineTranslation; /** * @var StoreManagerInterface */ protected $storeManager; /** * @param Context $context * @param TransportBuilder $transportBuilder * @param StateInterface $inlineTranslation * @param StoreManagerInterface $storeManager */ public function __construct( Context $context, TransportBuilder $transportBuilder, StateInterface $inlineTranslation, StoreManagerInterface $storeManager ) { $this->transportBuilder = $transportBuilder; $this->inlineTranslation = $inlineTranslation; $this->storeManager = $storeManager; parent::__construct($context); } /** * @return void */ public function execute() { $attachmentOne = [ 'name' => 'Attachment One', 'path' => 'path/to/attachment/one.pdf', 'type' => 'application/pdf' ]; $attachmentTwo = [ 'name' => 'Attachment Two', 'path' => 'path/to/attachment/two.pdf', 'type' => 'application/pdf' ]; try { $this->inlineTranslation->suspend(); $this->transportBuilder->setTemplateIdentifier('your_email_template_id') ->setTemplateOptions([ 'area' => 'frontend', 'store' => $this->storeManager->getStore()->getId() ]) ->setTemplateVars([]) ->setFrom([ 'email' => 'sender@example.com', 'name' => 'Sender Name' ]) ->addTo('recipient@example.com', 'Recipient Name') ->addMultipleAttachment([$attachmentOne, $attachmentTwo]) ->getTransport() ->sendMessage(); $this->inlineTranslation->resume(); $this->messageManager->addSuccess(__('Your email was sent successfully.')); } catch (\Exception $e) { $this->inlineTranslation->resume(); $this->messageManager->addError(__('There was an error sending your email. Please try again later.')); } return $this->_redirect('*/*/index'); } } ``` 以上代码将会发送带有两个附件的电子邮件。您可以根据自己的需要更改附件的数量和详细信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值