1

I'm using Magento 2.2. Order confirmation mail was sent when checkout button trigged. So, I disabled Order mail from the Backend. I only need to send order confirmation mail/invoice from my website itself automatically when payment is done. But I'm only getting mails from PayU about credit and debit payment details.

1

I am just extending @anime answer and writing order email code

In your module create events.xml in etc folder

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="checkout_onepage_controller_success_action">
       <observer name="process_gateway_redirect" instance="Vendor\Module\Observer\EmailSenderAfterPayment" />
   </event>
</config>

in your Observer

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

class EmailSenderAfterPayment implements ObserverInterface
{
    /**
     * @param Observer $observer
     * @return void
     */
    private $storeManager;
    protected $_checkoutSession;
    protected $sender;  
    protected $order;   

    public function __construct(
    StoreManagerInterface $storeManager,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\Order\Email\Sender\OrderSender $sender,
    \Magento\Sales\Model\Order $order

    ) {
        $this->storeManager = $storeManager;
        $this->_checkoutSession = $checkoutSession;
        $this->sender = $sender;
        $this->order = $order;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $orderIds = $observer->getOrderIds();
       $order = $this->order->load($orderIds[0]);
       $this->sender->send($order, true); //pass second param if you want to send the email asynchronous - otherwise it will be send by running corresponding cron job
       return;
    }
}
  • Added the code. But still not working. In order, notification is as order confirmation mail has not been sent. I need to set it as sent after mail sent. How do i test this code in local system because i connected with ERp if i made any changes it will cause erp inventory. – Tamil Selvan Jan 13 '18 at 6:42
  • Hello @murtuza $this->sender->send($order); this will not send order email to admin as BCC. How can I send to admin ? – chirag May 30 '18 at 13:58
  • @chirag check updated answer – Murtuza Zabuawala May 30 '18 at 16:23
  • 1
    Great it is working. – chirag Jun 1 '18 at 7:31
0

If you want to send the email only after the successful payment then you must write code in observer for checkout_onepage_controller_success_action event. Create app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="checkout_onepage_controller_success_action">
       <observer name="process_gateway_redirect" instance="Vendor\Module\Observer\EmailSenderAfterPayment" />
   </event>
</config>

Next create the observer at app/code/Vendor/Module/Observer/EmailSenderAfterPayment.php:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

class EmailSenderAfterPayment implements ObserverInterface
{
    /**
     * @param Observer $observer
     * @return void
     */
    private $storeManager;
    protected $_checkoutSession;


    public function __construct(
        StoreManagerInterface $storeManager,
        \Magento\Checkout\Model\Session $checkoutSession

    ) {
        $this->storeManager = $storeManager;
        $this->_checkoutSession = $checkoutSession;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    { 
        // do write the email sender functions right here.
        return;
    }
}

In case you do not know to send email programmatically refer this link.

  • Added the code, But still not working. In order, notification is as order confirmation mail has not been sent. I need to set it as sent after mail sent. How do i test this code in local system because i connected with ERp if i made any changes it will cause erp inventory. – Tamil Selvan Jan 13 '18 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.