How to Send Magento 2 Order Confirmation Email After Payment Success

by mageefy on Jan 3, 2023 Celebrities 389 Views

Default Magento automatically sends an order confirmation email once the order is placed. But what if someone wants to send an email after a successful payment completion from the third-party payment gateways in Magento 2 or what if the card is declined and payment fails? Unfortunately, Default Magento does not provide the functionality of sending email after getting successful payment from third-party gateways so we have come up with a custom code to send Email manually using event observer.

If you want to send the email only after the successful payment then you must write code in observer for checkout_onepage_controller_success_action event.

 

Create a simple method isEnable() using OrderIdentity.php to ensure that the order confirmation email is not sent twice.

Register the event under events.xml using the below code

[Package_Name]\[Module_Name]\etc\frontend\events.xml

 

<?xml version="1.0"?>

 

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="checkout_onepage_controller_success_action">

        <observer name="checkout_onepage_controller_success_action_sendmail" instance="[Package_Name]\[Module_Name]\Observer\SendMailOnOrderSuccess" />

    </event>

</config>

Once the event is registered, create Observer class in the following file:

[Package_Name]\[Module_Name]\Observer\SendMailOnOrderSuccess.php

 

 

 

<?php

 

namespace [Package_Name]\[Module_Name]\Observer;

 

use Magento\Framework\Event\ObserverInterface;

 

class SendMailOnOrderSuccess implements ObserverInterface

{

    /**

     * @var \Magento\Sales\Model\OrderFactory

     */

    protected $orderModel;

 

    /**

     * @var \Magento\Sales\Model\Order\Email\Sender\OrderSender

     */

    protected $orderSender;

 

    /**

     * @var \Magento\Checkout\Model\Session $checkoutSession

     */

    protected $checkoutSession;

 

    /**

     * @param \Magento\Sales\Model\OrderFactory $orderModel

     * @param \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender

     * @param \Magento\Checkout\Model\Session $checkoutSession

     *

     * @codeCoverageIgnore

     */

    public function __construct(

        \Magento\Sales\Model\OrderFactory $orderModel,

        \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender,

        \Magento\Checkout\Model\Session $checkoutSession

    )

    {

        $this->orderModel = $orderModel;

        $this->orderSender = $orderSender;

        $this->checkoutSession = $checkoutSession;

    }

 

    /**

     * @param \Magento\Framework\Event\Observer $observer

     * @return void

     */

    public function execute(\Magento\Framework\Event\Observer $observer)

    {

        $orderIds = $observer->getEvent()->getOrderIds();

        if(count($orderIds))

        {

            $this->checkoutSession->setForceOrderMailSentOnSuccess(true);

            $order = $this->orderModel->create()->load($orderIds[0]);

            $this->orderSender->send($order, true);

        }

    }

}

 

 

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.

[Package_Name]\[Module_Name]\etc\di.xml

 

<?xml version="1.0"?>

 

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Sales\Model\Order\Email\Container\OrderIdentity">

        <plugin name="change_is_enable_method" type="\[Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container\OrderIdentityPlugin"/>

    </type>

</config>

 

 

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

 

 

 

<?php

 

namespace [Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container;

 

class OrderIdentityPlugin

{

    /**

     * @var \Magento\Checkout\Model\Session $checkoutSession

     */

    protected $checkoutSession;

 

    /**

     * @param \Magento\Checkout\Model\Session $checkoutSession

     *

     * @codeCoverageIgnore

     */

    public function __construct(

        \Magento\Checkout\Model\Session $checkoutSession

    )

    {

        $this->checkoutSession = $checkoutSession;

    }

 

    /**

     * @param \Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject

     * @param callable $proceed

     * @return bool

     */

    public function aroundIsEnabled(\Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject, callable $proceed)

    {

        $returnValue = $proceed();

 

        $forceOrderMailSentOnSuccess = $this->checkoutSession->getForceOrderMailSentOnSuccess();

        if(isset($forceOrderMailSentOnSuccess) && $forceOrderMailSentOnSuccess)

        {

            if($returnValue)

                $returnValue = false;

            else

                $returnValue = true;

 

            $this->checkoutSession->unsForceOrderMailSentOnSuccess();

        }

 

        return $returnValue;

    }

}

That’s it! Now start sending the order confirmation Emails to your customers only after the payment is successful! Hope this code will help you to send Order Confirmation Email to your Customers after Successful Payment Transaction. You can even use & customize this code according to your need.

 

Related Best Magento Extension for your eCommerce Business- 

 

Bundle Product Option Image Extension For Magento 2

 

Reindex from Backend for Magento 2 Extension

 

Contact Us Inquiry Manager for Magento 2

 

Contact- 

 

Email  -  support@mageefy.com

              mageefy@gmail.com

 

Visit -  https://www.mageefy.com/

 

Address -  72, Second Floor, Ganesh Vihar Extension, Sirsi Road, Jaipur - 302034, India.

(+91) 0141- 6693741

 

 

 

 

Article source: https://article-realm.com/article/Arts-Entertainment/Celebrities/34450-How-to-Send-Magento-2-Order-Confirmation-Email-After-Payment-Success.html

Comments

No comments have been left here yet. Be the first who will do it.
Safety

captchaPlease input letters you see on the image.
Click on image to redraw.

Reviews

Guest

Overall Rating:

Most Viewed Articles

Statistics

Members
Members: 17068
Publishing
Articles: 79,458
Categories: 202
Online
Active Users: 1990
Members: 0
Guests: 1990
Bots: 23321
Visits last 24h (live): 3719
Visits last 24h (bots): 53655

Latest Comments

For anyone looking to start a business in Dubai Mainland. This is one of the most useful websites I’ve found for anyone who looking to start a business in Dubai The information about Dubai...
on Sep 20, 2026 about Josephine Linnea
My gym buddy kept talking about JokiCasino , so I checked it out. The sign-up was fast, no hoops. Australian users get proper AUD support, which is a relief. The loyalty program looks...
Every Client looking for high end companionship will find easy on the ears, gentle warmth, mastery in confident dialogue and a natural ease to make everyone at peace. Every client choosing for...
훌륭한 게시물과 리뷰에 감사드립니다. 이런 일을 계속하십시오. 보물섬 도메인 공식 주소  
 sometimes it feels like navigating a competitive space, much like playing Slither io where you need to stand out.
This is a useful overview of the annealing requirements for 304 stainless steel pipe. The explanation of the importance of temperature control, internal stress relief, and corrosion resistance is...
A prudent approach is to conduct a thorough investigation without haste. Listen for sounds that may provide useful clues, remember essential locations, and pay attention to  scary games peculiar...
Meera offers premium Aligarh call girl service with elegance and sophistication. Her stylish personality, engaging conversations, and positive attitude make every meeting enjoyable. She...
on Sep 16, 2026 about sonammathuri
Bài viết chia sẻ chi tiết về các chương trình phúc lợi của Chandrababu Naidu hay quá! Vừa tìm hiểu xong thông tin giải trí lướt vài chương truyện tại ưng tỷ comics  thì đúng chuẩn bài thư giãn....
Shikhsa provides quality Kolkata call girl service and social services focused on friendly interaction, conversation, and enjoyable experiences.     
on Sep 16, 2026 about sonammathuri

Translate To: