如何在magento2自定义电子邮件模板中添加密件抄送?

问题描述 投票:0回答:2

我正在使用以下代码发送邮件。邮件正常,但密件抄送和抄送不起作用。请告诉我如何设置密送和抄送。

 class Dummy
{

    private $_transportBuilder;

    public function __construct(\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder)
    {

        $this->_transportBuilder = $transportBuilder;
    }

    public function sendEmail($templateId = 1, $storeId = 1, $templateParams)
    {

        $transport = $this->_transportBuilder->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
            ->setTemplateVars($templateParams)
            ->setFrom('[email protected]')
            ->addTo('[email protected]')
            ->setReplyTo('[email protected]')
            ->addBcc('[email protected]')
            ->getTransport();
        $transport->sendMessage();
    }

}

提前致谢。

php html-email magento2.1
2个回答
0
投票

`if($custom_email){

$this->transportBuilder->addBcc($custom_email); // 添加为自定义动态电子邮件地址添加密件抄送

}`

这是更新的代码:

$transport = $this->_transportBuilder->setTemplateIdentifier($templateId)
        ->setTemplateOptions(['area' => Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId])
        ->setTemplateVars($templateParams)
        ->setFrom('[email protected]')
        ->addTo('[email protected]')
        ->setReplyTo('[email protected]')
        ->addBcc('[email protected]')
        ->getTransport();
    $transport->sendMessage();

0
投票

我们可以使用以下方法将密件抄送/抄送添加到任何电子邮件

创建插件

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Mail\Template\TransportBuilder">
      <plugin name="custom_welcome_email_bcc" type="Vendor\ModuleMane\Plugin\TransportBuilderPlugin" sortOrder="10"/>
    </type>
</config>

创建插件文件

<?php
namespace Vendor\ModuleName\Plugin;

class TransportBuilderPlugin
{
    protected $scopeConfig;
    
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->logger = $logger;
    }

    public function beforeSetTemplateIdentifier(
        \Magento\Framework\Mail\Template\TransportBuilder $subject,
        $templateIdentifier
    ) {
        $bccEmail = $this->scopeConfig->getValue('trans_email/ident_custom2/email');
        if( $templateIdentifier == 'customer_create_account_email_template') {
          if (!empty($bccEmail)) {
            $subject->addBcc($bccEmail);
          }
        }
        return [$templateIdentifier];
    }
}

这里的 customer_create_account_email_template 是电子邮件模板代码。

© www.soinside.com 2019 - 2024. All rights reserved.