How to Send Order Email to A Custom Email Address in Magento 2

In this blog post, I will give you the way to send order email to a custom email address in Magento 2 by using the custom code. The topic will cover all necessary operations by two following steps.

Overview of sending order email to a custom email address in Magento 2

  • Step 1: Set a form of the email
  • Step 2: Set a controller file

Step 1: Set a form of the email

Set the form of the email with the input field line as the following:

<form id="send-order-email" action="<?php $block->getUrl('helloworld/order/sendemail'); ?>">
    <label>Email Address</label>
    <input type="text" class="input-text" id="email">
</form>

Step 2: Set a controller file

File path: app\code\\Mageplaza\HelloWorld\Controller\Order\Email

Add the file app\code\\Mageplaza\HelloWorld\Controller\Order\Email that is based on class \Magento\Framework\App\Action\Action. The file will take the submit action from the above form, but firstly, please insert the action below:

public function execute()
    {
        $email = $this->getRequest()->getParam('email');
        $order = $this->_objectManager->create('Magento\Sales\Model\Order')->load(1); // this is entity id
        $order->setCustomerEmail($email);
        if ($order) {
            try {
                $this->_objectManager->create('\Magento\Sales\Model\OrderNotifier')
                    ->notify($order);
                $this->messageManager->addSuccess(__('You sent the order email.'));
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addError(__('We can\'t send the email order right now.'));
                $this->_objectManager->create('Magento\Sales\Model\OrderNotifier')->critical($e);
            }
        }
    }

I hope coding well with these guides when you start sending order email order email to a custom email address in Magento 2.



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
send
order-email
to
custom-email-address

Comments for How to Send Order Email to A Custom Email Address in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • magento 2 how to send order email to a custom email address