0

I need to send the order mail to people that are not the customer.

But the Order Sender doesn't send the email to more thant 1 person, why ?

use Magento\Sales\Model\Order\Email\Sender\OrderSender;

public function beforeExecute($subject, $observer)
{
    $event = $observer->getEvent();
    /** @var  \Magento\Sales\Model\Order $order */
    $order = $event->getOrder();
    ......
    $condition = false;

    if ($condition) {
        $order->setCanSendNewEmailFlag(false);
    }else{

        $customer = $this->_customerSession->getCustomer();
        if($customer->getGroupId() != "4"){

            $contacts = $this->getContacts();
            $contacts = array("mail@mail","other@mail");
            $order->setCustomerEmail($contacts[0]);
            $this->orderSender->send($order);
            $order->setCustomerEmail($contacts[1]);
            $this->orderSender->send($order);
        }
    }

    die;



    return [$observer];
}
0

You can use the CC option from admin config if the list is a fixed list. Go to "Store>Configuration>Sales>Sales Email" and look for "Send Order Email Copy To". You have the choice to send separate email and BCC

To do it correctly through program we should try to leverage the same pattern.

The class "\Magento\Sales\Model\Order\Email\Sender" calls sendCopyTo method after calling send.

    /**
 * Send order email if it is enabled in configuration.
 *
 * @param Order $order
 * @return bool
 */
protected function checkAndSend(Order $order)
{
    $this->identityContainer->setStore($order->getStore());
    if (!$this->identityContainer->isEnabled()) {
        return false;
    }
    $this->prepareTemplate($order);

    /** @var SenderBuilder $sender */
    $sender = $this->getSender();

    try {
        $sender->send();
    } catch (\Exception $e) {
        $this->logger->error($e->getMessage());
        return false;
    }
    try {
        $sender->sendCopyTo();
    } catch (\Exception $e) {
        $this->logger->error($e->getMessage());
    }
    return true;
}

If you see the sendCopyTo method its defined here:

\Magento\Sales\Model\Order\Email\SenderBuilder

on line 83.

/**
 * Prepare and send copy email message
 *
 * @return void
 */
public function sendCopyTo()
{
    $copyTo = $this->identityContainer->getEmailCopyTo();

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

            $this->transportBuilder->addTo($email);

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

and these methods are implemented here:

\Magento\Sales\Model\Order\Email\Container\OrderIdentity

In order to acheive your task programatically I think you should overwrite the 2 methods possibly using an after plugin and add your custom logic there:

  1. getEmailCopyTo
  2. getCopyMethod
0

I put together something for someone else but wasn't quite what they wanted.

Maybe this will work for you.

https://github.com/DominicWatts/EmailCC

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.