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

ego6inou  于 2023-09-29  发布在  PHP
关注(0)|答案(2)|浏览(93)

我用下面的代码发送邮件。邮件工作正常,但密件抄送和抄送不工作。请告诉我如何设置密件抄送和抄送。

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();
    }

}

先谢了。

mdfafbf1

mdfafbf11#

if($custom_email){
$this->transportBuilder->addBcc($custom_email);//为自定义动态电子邮件地址添加BCC
}`
以下是更新的代码:

$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();
omvjsjqw

omvjsjqw2#

我们可以使用以下方法将BCC/CC添加到任何电子邮件
创建插件

<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是电子邮件模板代码。

相关问题