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
- How to create a simple Hello World module for Magento 2
- Magento 2 Block Template Ultimate Guides
- How to Create Module in Magento 2
- How to Create Controller in Magento 2
- How to create CRUD Models in Magento 2
- How to Create Magento 2 Block, Layout and Templates
- Configuration - System.xml
- How To Create Admin Menu In Magento 2
- Admin ACL
- Admin Grid
People also searched for
- magento 2 how to send order email to a custom email address
- 2.3.x, 2.4.x