0

How to programmatically send an order confirmation email for two customer email addresses at once?

I created additional customer attribute for "contact email" field and need send confirmation to contact email and customer account email. How to it?

And I created Observer for sales_order_place_before event and wrote next code. But it's send only for contact email/customer account email. I need to send confirmation email to two emails at once.

<?php

declare(strict_types=1);

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Framework\Mail\Template\TransportBuilder;

/**
 * Class AfterOrderSave
 *
 * @package Vendor\Module\Observer
 */
class AfterOrderSave implements ObserverInterface
{
    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @var OrderSender
     */
    protected $orderSender;

    /**
     * @var TransportBuilder
     */
    protected $transportBuilder;

    public function __construct(
        RequestInterface $request,
        OrderSender $orderSender,
        TransportBuilder $transportBuilder
    ) {
        $this->request = $request;
        $this->orderSender = $orderSender;
        $this->transportBuilder = $transportBuilder;
    }

    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();

        $postParams = $this->request->getParams();
        $orderAccount = $postParams['order']['account'];
        $contactEmail = $orderAccount['id_contact_email'];
        $isContactEmailOnly = $orderAccount['id_contact_email_only'];

        if ($contactEmail && $isContactEmailOnly == true) {
            $this->transportBuilder->addTo($contactEmail);
        } else if ($contactEmail && $isContactEmailOnly == false) {
            $this->transportBuilder->addCc($contactEmail);
        }
    }
}
0

I did something similar with an extension

https://github.com/DominicWatts/EmailCC

Email is sent as CC

Hope that works for you. If not take a look at the source code and see how I did jt.

7
  • I added transportBuilder to my observer and try set contact email for transportBuilder->addCc() or transportBuilder->addBcc() methods but it's not work for me. What have I done wrong? – arturweb Mar 28 at 15:11
  • Difficult without seeing the code. Is it source code you can put on github? – Dominic Xigen Mar 28 at 18:24
  • I only added the Observer. I have attached my class code in the post message. This observer for 'sales_order_place_after' event. – arturweb Mar 28 at 18:44
  • transportBuilder would need to be completely rebuilt. It's not enough that you just include the CC unless you can catch it via the observer. But in this case your are not catching this so you need to include all the logic to rebuild. – Dominic Xigen Mar 28 at 20:32
  • Sorry, I'm new to Magento. Do you mean that I need to extend the transportBuilder class in my observer? Or how should I solve this problem? – arturweb Mar 28 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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