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.

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

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

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