定义邮件模板 (<Module>/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="module_hello_email_template" label="Test Mail" file="hello.html" type="html" module="Infinity_Base" area="frontend"/>
</config>

创建邮件模板 (<Module>/view/frontend/email/hello.html)

<!--@subject {{trans "Welcome to %store_name" store_name=$store.getFrontendName()}} @-->
<!--@vars {
"var this.getUrl($store, 'customer/account/')":"Customer Account URL",
"var customer.email":"Customer Email",
"var customer.name":"Customer Name"
} @-->
{{template config_path="design/email/header_template"}}

<p class="greeting">Hello {{trans "%name," name=$name}}</p>

{{template config_path="design/email/footer_template"}}

发出邮件

$templateId = 'module_hello_email_template';
$templateParams = ['name' => 'william'];

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/* @var \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder */
$transportBuilder = $objectManager->get('Magento\Framework\Mail\Template\TransportBuilder');
/* @var \Magento\Store\Model\StoreManagerInterface $storeManager */
$storeManager = $objectManager->get( 'Magento\Store\Model\StoreManagerInterface' );
/* @var \Magento\Customer\Helper\Session\CurrentCustomer $customer */
$customer = $objectManager->get( '\Magento\Customer\Helper\Session\CurrentCustomer' );

$transportBuilder->setTemplateIdentifier($templateId)
    ->setTemplateOptions(['area' => 'frontend', 'store' => $storeManager->getStore()->getId()])
    ->setTemplateVars($templateParams)
    ->setFrom('general') // sales,support,custom1,custom2
    ->addTo($customer->getCustomer()->getEmail(), $customer->getCustomer()->getFirstname())
    ->getTransport()
    ->sendMessage();

获取邮件模板代码

// template id, 通常在email_templates.xml定义。如果是在后台加的email template,需要换成template的记录ID,例如90
$identifier = 'contact_email_email_template';
/* @var \Magento\Framework\Mail\TemplateInterface $templateFactory */
$templateFactory = $this->_objectManager->create(
    'Magento\Framework\Mail\TemplateInterface',
    ['data' => ['template_id' => $identifier]]
);
// 模板变量,取决于phtml或后台email template的内容
$dataObject = new \Magento\Framework\DataObject();
$dataObject->setData('name', 'william');
// 决定模板变量取值区域,例如像{{layout}}这样的标签,如果取不到值可以试试把area设为frontend
$templateFactory->setOptions([
    'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID
]);
$templateFactory->setVars(['data' => $dataObject]);
return $templateFactory->processTemplate();

获取所有邮件模板的位置(powershell)

ls -Recurse vendor\magento\*\ | where {$_.Name -eq "email_templates.xml"} | foreach {
    $modulePath = $_.Directory.Parent.FullName
    $emailTemplatesXml = [xml](get-content $_)
    $emailTemplatesXml.config.template | foreach {
        $file = $_.GetAttribute("file")
        $area = $_.GetAttribute("area").toString()
        write-host "(Label):"$_.GetAttribute("label").toString()    "(ID):"$_.GetAttribute("id").toString()    "(file):"$modulePath\view\$area\email\$file
    }
}

关于模板变量

虽然通过email_templates.xml找到邮件模板很容易,但模板中使用的变量是PHP端提供的,模板变量只能在发送邮件的时候定义,所以如果要调整模板变量往往不太好找。以下列出自带组件含模板变量定义部分的代码位置

vendor/magento/module-sales/Model/Order/Email/Sender/*.php
vendor/magento/module-customer/Model/EmailNotification.php
vendor/magento/module-send-friend/Model/SendFriend.php

有趣的是order相关的email有event可以追加模板变量,例如email_order_set_template_vars_before,但并不是所有官方开发的邮件变量都有event可用。

官方的邮件变量定义代码都在Model里,但第三方组件就未必。其实也并不难找,只要对组件全代码搜索 ->setTemplateVars( ,就可以很容易找到。


猫之良品
2.5k 声望139 粉丝

资深Drupal, magento与Joomla


引用和评论

1 篇内容引用
0 条评论