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

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

23 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

4 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

1 week ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago