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!
How can I add custom email address and custom template for the same?
To fulfill your requirement you need to do custom code in SendMailOnOrderSuccess.php file, public function execute() available on Line no 46.
How can I add custom email address and custom template for the same?
To fulfill your requirement you need to do custom code in SendMailOnOrderSuccess.php file, public function execute() available on Line no 46.
Should i comment Default order Email ???
The [Package_Name]\[Module_Name]\etc\di.xml code itself stops email duplication. So you don’t need to comment anything.
Hi I have created above module as per your instruction.
I need instruction how & where i have to put this code.
Nice blog. U saved my time bro. Thanks for sharing.
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;
}
Thank you for notice, we updated the file path in blog.
Nice blog. U saved my time bro. Thanks for sharing.
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;
}
Thank you for notice, we updated the file path in blog.
Is there any solution for magento 1.9 or the same solution will work ?
Is there any solution for magento 1.9 or the same solution will work ?
The XML’s and code are a bit garbled.
What is supposed to be in the XML’s?
The XML’s and code are a bit garbled.
What is supposed to be in the XML’s?
Should i comment Default order Email ???
The [Package_Name][Module_Name]etcdi.xml code itself stops email duplication. So you don’t need to comment anything.
Hi I have created above module as per your instruction.
I need instruction how & where i have to put this code.
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.
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.
I dont want to stop Cash on delivery email when order placed. Is this code compatible with this?
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.
Hi,
This is not working for magento2.3.5-p2.
Please suggest solution.
Thank you
Can you please specify what issue you are facing?
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.
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
We have developed the free extension. You can try that one.
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.
We have developed the free extension. You can try that one.
But admin email is also blocked when i disable email in config, any solution for this?