Magento 2 email_templates.xml 用法解析

本文内容来自QQ群大佬陈伟明(一叶知秋)的学习分享

 

邮件模版

邮件相对重要,由以下几个部分组成

可以进到 Magento_Email:etc/email_templates.xsd 要看定义哪些属性

 

<?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="foggyline_helpdesk_email_template_customer"
label="Foggyline Helpdesk - Customer Email"
file="store_owner_to_customer.html" type="html"
module="Foggyline_Helpdesk" area="frontend"/>
<template id="foggyline_helpdesk_email_template_store_owner"
label="Foggyline Helpdesk - Store Owner Email"
file="customer_to_store_owner.html" type="html"
module="Foggyline_Helpdesk" area="frontend"/>
</config>

 

id的值是 config.xml中定义的 ,如:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<foggyline_helpdesk>
<email_template>
<customer>
foggyline_helpdesk_email_template_customer
</customer>
<store_owner>
foggyline_helpdesk_email_template_store_owner
</store_owner>
</email_template>
</foggyline_helpdesk>
</default>
</config>

 

模版

app/code/Foggyline/Helpdesk/view/frontend/email/customer_to_store_owner.html

<!--@subject New Ticket Created @-->
<h1>Ticket #{{var ticket.ticket_id}} created</h1>
<ul>
<li>Id: {{var ticket.ticket_id}}</li>
<li>Title: {{var ticket.title}}</li>
<li>Created_at: {{var ticket.created_at}}</li>
<li>Severity: {{var ticket.severity}}</li>
</ul>

 

在动作发邮件时会换变量

如:
//\Magento\Framework\Mail\Template\TransportBuilder 这是邮件处理类

$transport = $this->transportBuilder
->setTemplateIdentifier($this->scopeConfig
->getValue('foggyline_helpdesk/email_template/store_owner', $storeScope))
->setTemplateOptions(
[
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
'store' => $this->storeManager->getStore()->getId(),
]
)
->setTemplateVars(['ticket' => $ticket])
...

 

定义好,在 后台 Marketing -> Communication -> Email Template 就可以看到

发表评论