系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
在Magento 2中设置动态电子邮件主题的步骤:
步骤 1:在以下路径创建电子邮件模板配置文件
app\code\Vendor\Extension\etc\email_templates.xml
然后添加以下代码
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd"> <template id="your_email_template_id" label="Label of your template file" file="email_file.html" type="html" module="Vendor_Extension" area="frontend"/> </config>
步骤2:现在在以下路径创建电子邮件模板文件
app\code\Vendor\Extension\view\frontend\email\email_file.html
现在添加以下代码片段
<!--@subject {{var subject|raw }}@--> <!--@vars {"var customerName":"Customer Name", "var customerEmail":"Customer Email", "var customerComment":"Comment"} @--> <body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;"> <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;"> <table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%"> <tr> <td align="center" valign="top" style="padding:20px 0 20px 0"> <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;"> <tr> <td valign="top" colspan="5"> <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;">Hello Admin,</h1> </td> </tr> <tr> <td valign="top" colspan="5"> <p style="border:1px solid #E0E0E0; font-size:12px; line-height:16px; margin:0; padding:13px 18px; background:#F9F9F9;">You Have Received New Query As Bellow.<p> </td> </tr> <tr> <td valign="top" colspan="5"> <p style="border:1px solid #E0E0E0; font-size:12px; line-height:16px; margin:0; padding:13px 18px; background:#F9F9F9;">Customer Name : <strong>{{var customerName}}</strong></td> </tr> <tr> <td valign="top" colspan="5"> <p style="border:1px solid #E0E0E0; font-size:12px; line-height:16px; margin:0; padding:13px 18px; background:#F9F9F9;">Customer Email : <strong>{{var customerEmail}}</strong></td> </tr> <tr> <td valign="top" colspan="5"> <p style="border:1px solid #E0E0E0; font-size:12px; line-height:16px; margin:0; padding:13px 18px; background:#F9F9F9;">Customer Comment : <strong>{{var customerComment}}</strong></td> </tr> <tr> <td valign="top" colspan="5"> <p style="border:1px solid #E0E0E0; font-size:12px; line-height:16px; margin:0; padding:13px 18px; background:#F9F9F9; text-align:center;"><strong>Thank you.</strong></td> </tr> </table> </td> </tr> </table> </div> </body>
步骤3:现在模板已准备就绪,我们将编写发送邮件的代码。
转到控制器文件
app\code\Vendor\Extension\Controller\Index\Index.php
添加下面提到的代码
<?php namespace Vendor\Extension\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\Mail\Template\TransportBuilder; use Magento\Framework\Translate\Inline\StateInterface; class Index extends \Magento\Framework\App\Action\Action { protected $transportBuilder; protected $storeManager; protected $inlineTranslation; protected $state; public function __construct( Context $context, StoreManagerInterface $storeManager, TransportBuilder $transportBuilder, StateInterface $state) { $this->transportBuilder = $transportBuilder; $this->storeManager = $storeManager; $this->inlineTranslation = $state; parent::__construct($context); } public function execute() { $templateId = 'your_email_template_id'; // template id $fromEmail = 'admin@gmail.com'; // sender Email id $toEmail = 'test.magecomp@gmail.com'; // receiver email id $subject = 'your_dynamic_subject'; // Dynamic subject try { // template variables pass here $templateVars = [ 'subject' => $subject, 'customerName' => 'Test', 'customerEmail' => 'Magecomp', 'customerComment' => 'Test Comment' ]; $storeId = $this->storeManager->getStore()->getId(); $this->inlineTranslation->suspend(); $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; $templateOptions = [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId ]; $transport = $this->transportBuilder->setTemplateIdentifier($templateId, $storeScope) ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom($fromEmail) ->addTo($toEmail) ->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); $this->messageManager->addSuccessMessage(__('Your Email Sent successfully')); $this->_redirect('*/*/'); } catch (\Exception $e) { $this->inlineTranslation->resume(); $this->messageManager->addErrorMessage(__('We can\'t process your request' . $e->getMessage())); $this->_redirect('*/*/'); } } }
结论:
这样,您可以在Magento 2中设置动态电子邮件主题,并提高电子邮件营销活动的打开率。