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!