How to send a Order Email to a Custom Email Address

The following steps shows you to send a order email to a custom Email in Magento 2

Step 1: built a form by an Email input field  line

Step 2: built a file “app\code\\Webnexs\TipAndTrick\Controller\Order\Email”

 In magento admin page it only supports send Email to the Email address in order. So if you want to send order Email to Email address you need to write a custom code to do it.

Create an form with a Email Input field in the below

<form id="send-order-email" action="<?php $block->getUrl('tipandtrick/order/sendemail'); ?>">

    <label>Email Address</label>

    <input type="text" class="input-text" id="email">

</form>

namespace Webnexs\TipAndTrick\Controller\Order;

class Email extends \Magento\Framework\App\Action\Action

Then built a file “app\code\\Webnexs\TipAndTrick\Controller\Order\Email

which Extends class

\Magento\Framework\App\Action\Action

to grab a submit action of above form

I write an execute function to execute to execute submit form action , the following code :

public function execute()

    {

        $email = $this->getRequest()->getParam('email');

        $order = $this->_objectManager->create('Magento\Sales\Model\Order')->load(1);

        $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 the above mentioned step helps you to send an Order Email to a custom Email .


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *