Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Send Order Email to A Custom Email Address in Magento 2

Vinh Jacker | 12-18-2024

Send Order Email to A Custom Email Address

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

x
    Jacker

    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.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services