Magento - Custom subject of order email

Magento - Custom subject of order email

August 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Custom subject of order email

Customizing Magento Order Email Subjects

When interacting with a Magento website using a PHP service, you may find that certain aspects of the system, like order emails, are not easily customizable without direct access to the source code. However, with some strategic approach and understanding of Magento’s architecture, it is possible to modify the subject lines of order emails.

The Default Email Subject

By default, when an order is placed in Magento, the sendNewOrderEmail() method triggers the system to send a notification email to the specified recipient. The subject line of this email is generated using the core Magento code and follows the format: “Nuovo ordine # XXXXXXXXX”.

Using Magento’s Observer Pattern

Magento utilizes the observer pattern for many of its events, including order placement. To modify the subject line without altering the core code directly, we can create an observer that intercepts the sales_order_place_after event and then manipulates the email subject accordingly.

Here’s a step-by-step guide to achieving this:

  1. Create an Observer

First, you need to create your own observer class that implements the required Magento observer interface. This class will be responsible for processing the event when an order is placed successfully.

class CustomOrderEmailObserver implements \Magento\Framework\Event\ObserverInterface
{
    protected $_transportBuilder;

    public function __construct(
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
    ) {
        $this->_transportBuilder = $transportBuilder;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getOrder();
        $templateId = 'sales_email_order_new';
        $sender = [
            'name' => 'General Contact',
            'email' => 'general@domain.com'
        ];

        $transport = $this->_transportBuilder
            ->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $order->getStoreId()])
            ->setTemplateVars(['order' => $order])
            ->addTo($order->getCustomerEmail(), $order->getCustomerName())
            ->setFromByStore($sender);

        $transport->sendMessage();

        // This is where the subject manipulation occurs
        $transport->getTransport()->getMail()->Subject = 'TEST - Nuovo ordine # ' . $order->getIncrementId();
    }
}
  1. Configure the Observer

Next, you need to configure Magento to use your observer when the sales_order_place_after event is triggered. This involves adding your observer’s class name to the appropriate XML configuration file.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_place_after">
        <observer instance="CustomOrderEmailObserver" />
    </event>
</config>

Remember to replace CustomOrderEmailObserver with the actual class name you used in step 1.

Conclusion

While Magento’s core code is not easily editable without direct access, utilizing observers provides a clean and maintainable way of extending Magento’s functionality. By creating an observer that intercepts the order placement event, we can effectively modify the subject line of the order confirmation email to include our desired prefix (“TEST -”).

This approach allows us to keep the core code intact while still customizing the behavior of the system according to our specific needs.

For more in-depth tutorials on Magento development and best practices, be sure to check out the laravelcompany.com blog.

Extensions Email Orders

Our network