How to Send Order Confirmation Email After Successful Payment in Magento 2

How to Send Order Confirmation Email to Customers after Successful Payment in Magento 2

Magento automatically sends various emails for registration, order, refunds and many more. Generally, Magento auto sends an email once the order is placed but what if someone wants to send email after a successful payment completion from the third-party payment gateways in Magento 2? one of our client came up with the same requirement of sending an order confirmation email after successful order payment.

Unfortunately, Default Magento does not provide functionality of sending email after getting successful payment from third-party gateways, and to nail it, we have come up with custom code to send Email manually using observer.

To send email confirmation on successful order payment, we need to tie Magento 2 observer with an event of checkout_onepage_controller_success_action.
Here, we need to ensure that order confirmation email is not sent twice, So we need to create a simple method isEnable() using OrderIdentity.php.

First, we need to register event under events.xml using below code
[Package_Name]\[Module_Name]\etc\frontend\events.xml




    
        
    

Once event is registered, we need to create Observer class in following file.
[Package_Name]\[Module_Name]\Observer\SendMailOnOrderSuccess.php


[Package_Name]\[Module_Name]\etc\di.xml



    
        
    

And Lastly, you need to create another file along with ‘di.xml’ that is OrderIdentityPlugin.php to remove duplication of email.
[Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container\ OrderIdentityPlugin.php


Note: To make use of above code or extension, you must disable the default Order Email from your Magento 2 store backend to enjoy the extension/above code functionality. Simply, navigate to Store -> Configuration -> Sales -> Sales email -> Order -> and set default Enabled option to “No” as shown below.
Magento 2 Order Confirmation Email Extension CTA
Let me know if you face any issue using this code to send an order confirmation Email. Queries, suggestions and feedback are always welcomed through commenting.
Happy Mailing!
Previous Article

How to Add Signature Image in Magento Invoice PDF

Next Article

How to Setup & Use High Performance Redis In Magento 2 Database

Write a Comment
    1. To fulfill your requirement you need to do custom code in SendMailOnOrderSuccess.php file, public function execute() available on Line no 46.

  1. ABHISEKH RANJAN

    Hi I have created above module as per your instruction.
    I need instruction how & where i have to put this code.

  2. Hi,

    You did mistakes in your blog, [Package_Name][Module_Name]PluginSalesOrderOrderEmailContainer OrderIdentityPlugin.php should be [Package_Name][Module_Name]PluginSalesOrderEmailContainer OrderIdentityPlugin.php

    and OrderIdentityPlugin.php file function should be below code,

    public function aroundIsEnabled(MagentoSalesModelOrderEmailContainerOrderIdentity $subject, callable $proceed)
    {
    $returnValue = $proceed();
    $returnValue = false;
    $forceOrderMailSentOnSuccess = $this->checkoutSession->getForceOrderMailSentOnSuccess();
    if(isset($forceOrderMailSentOnSuccess) && $forceOrderMailSentOnSuccess)
    {
    //if($returnValue)
    // $returnValue = false;
    //else
    $returnValue = true;
    $this->checkoutSession->unsForceOrderMailSentOnSuccess();

    }
    return $returnValue;
    }

  3. Hi,

    You did mistakes in your blog, [Package_Name]\[Module_Name]\Plugin\Sales\Order\Order\Email\Container\ OrderIdentityPlugin.php should be [Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container\ OrderIdentityPlugin.php

    and OrderIdentityPlugin.php file function should be below code,

    public function aroundIsEnabled(\Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject, callable $proceed)
    {
    $returnValue = $proceed();
    $returnValue = false;
    $forceOrderMailSentOnSuccess = $this->checkoutSession->getForceOrderMailSentOnSuccess();
    if(isset($forceOrderMailSentOnSuccess) && $forceOrderMailSentOnSuccess)
    {
    //if($returnValue)
    // $returnValue = false;
    //else
    $returnValue = true;
    $this->checkoutSession->unsForceOrderMailSentOnSuccess();

    }
    return $returnValue;
    }

    1. The [Package_Name][Module_Name]etcdi.xml code itself stops email duplication. So you don’t need to comment anything.

  4. ABHISEKH RANJAN

    Hi I have created above module as per your instruction.
    I need instruction how & where i have to put this code.

  5. Matias Garcia

    Is there any way to do this without using the checkout_onepage_controller_success_action event? We are using it to do other important task

    Regards!

    1. No without that one it may not possible.
      No issues if you use the same events for the other tasks, you can put above code with your that code, both should work normally.

  6. Sarvesh Tiwari

    Hello Support Team,
    I am really thankful for this extension. it is working fine from fronend but it is not working from backend.
    can you provide any solutions for backend.

  7. Is it necessary to disable default Order option to “No” under Sales Emails Section?
    because mail is not working during online payment complete process .

      1. Hi, I use Magento ver. 2.3.5-p1 and it doesn’t work for me too.
        it gets right order on SendMailOnOrderSuccess.php line 52.
        But email is not sending.

        regular order confirmations are sending fine.

  8. Hi,
    I have not received the Order Confirmation Email After Successful Payment, i am using 2.4.2 Magento version
    Only payu email i have received can you tell me what is the issue
    COD & Bank transfer email working

  9. I have tried your solution, still m getting the email triggering twice one at checkout and another at payment success.
    On Disable Default Email from the Admin Back end, It’s stop sending email at both checkout and on payment success.

    Your Quick response will be appreciated as we are stuck with this to make the Site Live.

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨