Magento – Magento 2 How to i add BCC to order email with observer programatically

customeremailevent-observermagento2orders

I have created module and wrote one an observer on "order_place_after", here i want to send the same order email copy to my custom email address.

How can i send the same order email to my custom email address or add that email address as BCC to send order email copy?

Thanks

Best Answer

You can managed to work it by follwoing code:

Step 1: Create a module with name Vendor_Module and do the following changes.

Step 2: create di.xml under

app/code/Vendor/Module/etc/di.xml

with content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Email\SenderBuilder" type="Vendor\Module\Model\Order\Email\SenderBuilder" />
</config>

Step 3: Create SenderBuilder.php under

app/code/Vendor/Module/Model/Order/Email/SenderBuilder.php

with content:

<?php
namespace Vendor\Module\Model\Order\Email;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\Template\TransportBuilderByStore;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;

class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
    protected $templateContainer;
    protected $identityContainer;
    protected $transportBuilder;
    private $transportBuilderByStore;

    public function __construct(
        Template $templateContainer,
        IdentityInterface $identityContainer,
        TransportBuilder $transportBuilder,
        TransportBuilderByStore $transportBuilderByStore = null
    ) {
        $this->templateContainer = $templateContainer;
        $this->identityContainer = $identityContainer;
        $this->transportBuilder = $transportBuilder;
        $this->transportBuilderByStore = $transportBuilderByStore ?: ObjectManager::getInstance()->get(
            TransportBuilderByStore::class
        );
        parent::__construct($templateContainer, $identityContainer, $transportBuilder, $this->transportBuilderByStore);
    }

    public function send()
    {
        $custom_email = 'YOUR_DYNAMIC_EMAIL_ADDRESS';

        $this->configureEmailTemplate();

        $this->transportBuilder->addTo(
            $this->identityContainer->getCustomerEmail(),
            $this->identityContainer->getCustomerName()
        );

        $copyTo = $this->identityContainer->getEmailCopyTo();

        if (!empty($copyTo) && $this->identityContainer->getCopyMethod() == 'bcc') {
            foreach ($copyTo as $email) {
                $this->transportBuilder->addBcc($email);
            }
        }

        if($custom_email){
            $this->transportBuilder->addBcc($custom_email); // Added for add BCC for custom dynamic email address
        }

        $transport = $this->transportBuilder->getTransport();
        $transport->sendMessage();
    }
}

And now run required commands like setup:upgrade, di compile etc. You can modify the codes according to your requirement.

Note: Above codes are not Tested but it should work.

Related Topic