{"id":113,"date":"2018-06-06T09:11:15","date_gmt":"2018-06-06T09:11:15","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/06\/06\/send-magento-2-order-confirmation-email-after-payment-success\/"},"modified":"2025-05-22T17:30:15","modified_gmt":"2025-05-22T12:00:15","slug":"send-magento-2-order-confirmation-email-after-payment-success","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/send-magento-2-order-confirmation-email-after-payment-success\/","title":{"rendered":"How to Send Magento 2 Order Confirmation Email After Payment Success"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the normal scenario, after you&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/set-up-order-confirmation-email-in-magento-2\/\">set up order confirmation Email in the Magento 2<\/a>&nbsp;customers receive order confirmation Email immediately after the order is placed. As online purchases don\u2019t allow them to get their products instantly, the confirmation gives them the patience to wait for the delivery after they have spent their money! Such a service is helpful for customer satisfaction and hence improving the customer experience of your store!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Default Magento automatically&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/set-up-order-confirmation-email-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">sends a confirmation Email<\/a>&nbsp;once the order is placed. But the question is what if the payment is done via a third party payment gateway or what if the card is declined and payment fails? Default Magento 2 falls short in such a situation. So I have come up with custom code to allow to&nbsp;send order confirmation Email after payment success in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To send email confirmation on successful order payment, we need to tie Magento 2 observer with an event of&nbsp;<strong>checkout_onepage_controller_success_action<\/strong>.<br>Create a simple method&nbsp;<strong>isEnable()<\/strong>&nbsp;using&nbsp;<strong>OrderIdentity.php&nbsp;<\/strong>to ensure that order confirmation email is not sent twice.<strong>&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Register event under events.xml using below code<br><strong><strong>[Package_Name]\\[Module_Name]\\etc\\frontend\\events.xml<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\">\n \n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">\n    &lt;event name=\"checkout_onepage_controller_success_action\">\n        &lt;observer name=\"checkout_onepage_controller_success_action_sendmail\" instance=\"[Package_Name]\\[Module_Name]\\Observer\\SendMailOnOrderSuccess\" \/>\n    &lt;\/event>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once the event is registered, create Observer class in the following file:<br><strong><strong>[Package_Name]\\[Module_Name]\\Observer\\SendMailOnOrderSuccess.php<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n \nnamespace [Package_Name]\\[Module_Name]\\Observer;\n \nuse Magento\\Framework\\Event\\ObserverInterface;\n \nclass SendMailOnOrderSuccess implements ObserverInterface\n{\n    \/**\n     * @var \\Magento\\Sales\\Model\\OrderFactory\n     *\/\n    protected $orderModel;\n \n    \/**\n     * @var \\Magento\\Sales\\Model\\Order\\Email\\Sender\\OrderSender\n     *\/\n    protected $orderSender;\n \n    \/**\n     * @var \\Magento\\Checkout\\Model\\Session $checkoutSession\n     *\/\n    protected $checkoutSession;\n \n    \/**\n     * @param \\Magento\\Sales\\Model\\OrderFactory $orderModel\n     * @param \\Magento\\Sales\\Model\\Order\\Email\\Sender\\OrderSender $orderSender\n     * @param \\Magento\\Checkout\\Model\\Session $checkoutSession\n     *\n     * @codeCoverageIgnore\n     *\/\n    public function __construct(\n        \\Magento\\Sales\\Model\\OrderFactory $orderModel,\n        \\Magento\\Sales\\Model\\Order\\Email\\Sender\\OrderSender $orderSender,\n        \\Magento\\Checkout\\Model\\Session $checkoutSession\n    )\n    {\n        $this->orderModel = $orderModel;\n        $this->orderSender = $orderSender;\n        $this->checkoutSession = $checkoutSession;\n    }\n \n    \/**\n     * @param \\Magento\\Framework\\Event\\Observer $observer\n     * @return void\n     *\/\n    public function execute(\\Magento\\Framework\\Event\\Observer $observer)\n    {\n        $orderIds = $observer->getEvent()->getOrderIds();\n        if(count($orderIds))\n        {\n            $this->checkoutSession->setForceOrderMailSentOnSuccess(true);\n            $order = $this->orderModel->create()->load($orderIds[0]);\n            $this->orderSender->send($order, true);\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After adding observer code, an email will be sent to customers on successful order payment. To avoid order duplication Email, we need to create a plugin using the di.xml file.<br><strong><strong>[Package_Name]\\[Module_Name]\\etc\\di.xml<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n \n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Sales\\Model\\Order\\Email\\Container\\OrderIdentity\">\n        &lt;plugin name=\"change_is_enable_method\" type=\"\\[Package_Name]\\[Module_Name]\\Plugin\\Sales\\Order\\Email\\Container\\OrderIdentityPlugin\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create another file along with \u2018di.xml\u2019 that is&nbsp;<strong>OrderIdentityPlugin.php<\/strong>&nbsp;to remove duplication of email.<br><strong><strong>[Package_Name]\\[Module_Name]\\Plugin\\Sales\\Order\\Email\\Container\\ OrderIdentityPlugin.php<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n \nnamespace [Package_Name]\\[Module_Name]\\Plugin\\Sales\\Order\\Email\\Container;\n \nclass OrderIdentityPlugin\n{\n    \/**\n     * @var \\Magento\\Checkout\\Model\\Session $checkoutSession\n     *\/\n    protected $checkoutSession;\n \n    \/**\n     * @param \\Magento\\Checkout\\Model\\Session $checkoutSession\n     *\n     * @codeCoverageIgnore\n     *\/\n    public function __construct(\n        \\Magento\\Checkout\\Model\\Session $checkoutSession\n    )\n    {\n        $this->checkoutSession = $checkoutSession;\n    }\n \n    \/**\n     * @param \\Magento\\Sales\\Model\\Order\\Email\\Container\\OrderIdentity $subject\n     * @param callable $proceed\n     * @return bool\n     *\/\n    public function aroundIsEnabled(\\Magento\\Sales\\Model\\Order\\Email\\Container\\OrderIdentity $subject, callable $proceed)\n    {\n        $returnValue = $proceed();\n \n        $forceOrderMailSentOnSuccess = $this->checkoutSession->getForceOrderMailSentOnSuccess();\n        if(isset($forceOrderMailSentOnSuccess) &amp;&amp; $forceOrderMailSentOnSuccess)\n        {\n            if($returnValue)\n                $returnValue = false;\n            else\n                $returnValue = true;\n \n            $this->checkoutSession->unsForceOrderMailSentOnSuccess();\n        }\n \n        return $returnValue;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There you go!<br>Now you may start sending the order confirmation Emails to your customers only after the payment is successful. ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Start Emailing today itself. We\u2019d love to hear how your order Emails are now organized in a better way.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the normal scenario, after you&nbsp;set up order confirmation Email in the Magento 2&nbsp;customers receive order confirmation Email immediately after the order is placed. As&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":15652,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions\/15652"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}