Programmatically Triggering Email Sends in Magento
In Magento, sending tracking information emails is typically initiated through the admin interface by clicking the “Send Tracking Information” button on an order’s page. This action triggers an event that, in turn, fires off a new email to the customer.
However, if you’re using an external integration like eBridge to populate shipping data and would like Magento to automatically send these emails without manual intervention, you’ll need to handle this programmatically. Here’s how:
Identifying the Trigger
The key is to find the event that Magento listens for when the “Send Tracking Information” button is clicked. This event likely occurs when the tracking number field is updated or saved.
To identify it, navigate to the app/code/core/Mage/Sales/Model/Order.php file. You’ll need to look for methods related to order status updates and email sending.
Handling the Event
You can create an observer to listen for this event and then trigger the email send yourself using Magento’s built-in email functionality. Here’s a basic example of how you might set up your observer:
<config>
<global>
<models>
<emailobserver>
<class>EmailObserver_Model_Observer</class>
</emailobserver>
</models>
<events>
<sales_order_save_after>
<observers>
<send_email_after_order_save>
<type>singleton</type>
<class>emailobserver/observer</class>
<method>processOrderSaveAfter</method>
</send_email_after_order_save>
</observers>
</sales_order_save_after>
</events>
</global>
</config>
In the observer class (EmailObserver_Model_Observer), you’d implement a method like this:
public function processOrderSaveAfter($observer)
{
$order = $observer->getEvent()->getOrder();
// Check if the order has been shipped recently and tracking info added
if ($order->getIsShippingAssigned() && !empty($order->getShippingAssignments())) {
try {
$this->_sendTrackingEmail($order);
Mage::log('Tracking email sent for order ' . $order->getIncrementId());
} catch (Exception $e) {
Mage::logException($e);
}
}
}
The _sendTrackingEmail method would use Magento’s email template system to build and send the tracking information email.
Conclusion
To summarize, programmatically triggering an email in Magento requires identifying the event that fires when you manually click “Send Tracking Information”. You can then create an observer to listen for this event and handle it by sending the email yourself using Magento’s built-in email functionality.
This approach ensures your system remains integrated with Magento while allowing external tools like eBridge to automatically initiate customer notifications without manual intervention in the admin interface.
For more detailed information on Magento email templates, events, and observers, I recommend checking out our comprehensive guide at Laravel Company.
Happy coding!