3

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

2
  • Why you want it through observer? You can override the order email or use plugin. Dec 19, 2018 at 10:12
  • i have module which has functionality to configure email addresses to each customers from admin side. so when that customer order something then the configured email address also will get same notification. Dec 19, 2018 at 10:19

1 Answer 1

6

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.

5
  • how should i use with my observer, as i have already created one observer. Dec 19, 2018 at 11:14
  • @UtsavGupta add your code in question will surely help you out Dec 19, 2018 at 11:16
  • anyone know how I can achieve this but with magento 1.9 Jan 8, 2020 at 11:14
  • 1
    Hello, can I load order here ? I want to add bcc but it is base on some order value. May I get Order in this same function ? Apr 20, 2020 at 11:29
  • @Sukumar Gorai: what do you think plugin can work on this class instead preference. Jun 15, 2021 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.