magento2 email

定義郵件模板 (<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端提供的,模板變量只能在發送郵件的時候定義,因此若是要調整模板變量每每不太好找。如下列出自帶組件含模板變量定義部分的代碼位置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可用。html

官方的郵件變量定義代碼都在Model裏,但第三方組件就未必。其實也並不難找,只要對組件全代碼搜索 ->setTemplateVars( ,就能夠很容易找到。shell

相關文章
相關標籤/搜索