Chapters Close

How to send order email to a custom email address?

This piece of information will throw some light on to how to send an order email to a custom email address in Magento 2.

To achieve this, you just have to follow a couple of steps mentioned below:

  • Step 1: Set an email form
  • Step 2: Create a Controller file


Step 1: Set an email form

Set the email form with the input field line as given below:

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

Step 2: Create a Controller file

Create a controller file at the path given below and add the following code: app\code\Aureatelabs\DemoModule\Controller\Order\Email
This file is based on the class \Magento\Framework\App\Action\Action and it will take the submit action from the form created in the first step.

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);
       }
   }
}

Feel free to leave the comments below and contact us if you need any help to customize your Magento store.

Happy coding!

Speak your Mind

Post a Comment

Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️

* This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Grow your online business like 4,432 subscribers

    * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
    envelope

    Thank You!

    We are reviewing your submission, and will be in touch shortly.