I override the \Magento\Sales\Model\Order\Email\SenderBuilder class in my Magento 2. I want to load some order data but I am not getting anything.

Code :

namespace Customisation\OrderEmail\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(
        parent::__construct($templateContainer, $identityContainer, $transportBuilder, $this->transportBuilderByStore);


    public function send()
        //$custom_email = '[email protected]';
        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();


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

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

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

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

I want to get order data in this send(). How can I get order id or Order in this function ?

1 Answer 1


There should be order instance in the template variables.

$vars = $this->templateContainer->getTemplateVars();
$order = $vars['order'];

Might be slightly different depending on the version you are running. Check the prepareTemplate method in Magento\Sales\Model\Order\Email\Sender class.

  • Thank you. But I am getting following error Uncaught Error: Call to undefined method getTemplateVars(). My magento version is 2.3.2 Apr 20, 2020 at 12:02
  • I've updated the answer. @DavidCoder Apr 20, 2020 at 12:32
  • Now getting error: Call to a member function getOrder() on array Apr 20, 2020 at 12:38
  • getting above error in $order = $vars->getOrder(); line Apr 20, 2020 at 12:42
  • 1
    Hmm, I thought it was DataObject for some reason. Apr 20, 2020 at 12:48

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.