Categories: How ToMagento 2

How to Send Order Confirmation Email 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.

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!
Click to rate this post!
[Total: 34 Average: 4.1]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • 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.

  • 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

      • 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.

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

    • Yes otherwise you will receive 2 email notifications.
      So this can work mainly if you have only online payment methods enabled.

  • 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.

  • 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!

    • 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.

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

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

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

9 hours ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

2 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

2 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

4 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

6 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago