What event is Magento using for sending out order confirmation mails

What event is Magento using for sending out order confirmation mails

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
What event is Magento using for sending out order confirmation mails

Solving Magento Order Confirmation Email Event for Observers

Hello developers! Today we’re going to dive into a common challenge when working with Magento observers – understanding and utilizing the event that triggers order confirmation emails.

The Problem: Multiple Payment States and Observer Triggers

Our developer friend is having trouble with an observer that uploads XML data and sends admin notifications. They have tried two events:

  1. <sales_order_place_after> which only fires once per order, but doesn’t handle scenarios like credit card payments being captured after processing.
  2. <sales_order_save_after> which triggers on every order update, leading to multiple unnecessary uploads.

The issue lies in the two payment options offered – bank transfer (which immediately goes to [New] status) and credit card (which first processes, then captures the payment before finalizing as [New]). This leads to the observer firing at different stages depending on the payment method.

The Solution: Using Magento’s Order Confirmation Email Event

To solve this, we want to use the same event that Magento uses to send out order confirmation mails. This event will ensure our observer only fires once per order, after all necessary steps are completed.

The event you’re looking for is:

<sales_email_order_set_template_after>

This event triggers after an email template has been set for an order – which happens just before the email is sent out.

Writing the Correct Observer

Let’s modify your existing observer to use this new event. First, update your configuration to match the new event:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/event.xsd">
    <event name="sales_email_order_set_template_after">
        <observer name="your_module_observer" instance="Your\Module\Observer\SalesOrderSaveAfter" shared="false"/>
    </event>
</config>

Then, update your observer:

public function salesEmailOrderSetTemplateAfter($observer)
{
    $order = $observer->getEvent()->getOrder();
    $orderStatus = $order->getStatus();

    if ($orderStatus == 'pending' || $orderStatus == 'bank_transfer') {
        // Your XML upload and admin notification logic here
        ...
    }
}

This observer will now only trigger after the order has been finalized, regardless of whether it was a bank transfer or credit card payment.

Conclusion

Understanding Magento’s core event triggers is key to writing efficient observers. By using <sales_email_order_set_template_after>, we’ve ensured that our observer fires at the correct stage – after all necessary actions are completed and just before the order confirmation email is sent out.

Remember, this isn’t a built-in event in Magento 2 – you’ll need to add it manually. For detailed guides on working with observers, check out the official Magento DevDocs or our comprehensive tutorials at LaravelCompany.

Happy coding! Let me know if you have any other questions.

Disclaimer: Always ensure you’re following best practices and maintaining proper version control when working with Magento core files.

Extensions Themes Admin Email

Our network