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 an order email to a custom email address in Magento 2 by using the custom code.

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.

2 Steps to send order email to a custom email address in Magento 2

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 you will be coding well with these guides when you start sending order email to a custom email address in Magento 2. If you have any issue while following this tutorial, feel free to let me know and I will help you figure it out as soon as possible.

Related Topics

Enjoyed the tutorial? Spread it to your friends!

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

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 how to send order email to a custom email address
  • 2.2.x, 2.3.x, 2.4.x