Magento2发送Email的步骤

以发送重置密码邮件为例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// file: vendor/magento/module-customer/Model/EmailNotification.php
/**
 * Send email to customer when his password is reset
 *
 * @param CustomerInterface $customer
 * @return void
 */
private function passwordReset(CustomerInterface $customer)
{
    $storeId = $customer->getStoreId();
    if (!$storeId) {
        $storeId = $this->getWebsiteStoreId($customer);
    }

    $customerEmailData = $this->getFullCustomerObject($customer);

    $this->sendEmailTemplate(
        $customer,
        self::XML_PATH_RESET_PASSWORD_TEMPLATE,
        self::XML_PATH_FORGOT_EMAIL_IDENTITY,
        ['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
        $storeId
    );
}

/**
 * Send corresponding email template
 *
 * @param CustomerInterface $customer
 * @param string $template configuration path of email template
 * @param string $sender configuration path of email identity
 * @param array $templateParams
 * @param int|null $storeId
 * @param string $email
 * @return void
 */
private function sendEmailTemplate(
    $customer,
    $template,
    $sender,
    $templateParams = [],
    $storeId = null,
    $email = null
) {
    $templateId = $this->scopeConfig->getValue($template, 'store', $storeId);
    if ($email === null) {
        $email = $customer->getEmail();
    }
    $transport = $this->transportBuilder->setTemplateIdentifier($templateId)
        ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
        ->setTemplateVars($templateParams)
        ->setFrom($this->scopeConfig->getValue($sender, 'store', $storeId))
        ->addTo($email, $this->customerViewHelper->getCustomerName($customer))
        ->getTransport();

    $transport->sendMessage();
}
Built with Hugo
主题 StackJimmy 设计