Send shipment email programmatically for the completed orders in magento

Send shipment email programmatically for the completed orders in magento

July 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Send shipment email programmatically for the completed orders in magento

Sending Shipment Emails for Completed Orders in Magento

Hello! I’m glad you’re looking to automate the shipment email process for your completed orders in Magento. Let’s walk through this step-by-step.

The Current Scenario

You mentioned that your current script works fine for processing orders but not for completed ones. It also doesn’t include order item details in the email. This is likely due to how Magento handles order states and shipping information.

Understanding Magento Order States

Magento has several order states, including pending, processing (the default after an order is placed), complete (after all items have been shipped), and closed/closed/cancelled (if the order is canceled). Your script is probably catching orders only in the “processing” state.

The Solution: Use Mage_Sales_Model_Order_Shipment

To send shipment emails for completed orders, you should use Mage::getModel('sales/order_shipment') instead of Mage::getModel('sales/service_order', $order).

Here’s an updated version of your script:

function sendShipmentEmail($orderIncrementId, $shipmentTrackingNumber = null) {
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
    if (!$order->getId()) {
        throw new Exception("Order does not exist");
    }

    // Check if there are any items left to ship
    $itemsToShip = Mage::helper('core')->jsonDecode($order->getData('items_to_ship'));
    if (empty($itemsToShip)) {
        return;  // No items to ship for this order
    }

    // Prepare shipment data
    $shipmentData = array(
        'entity_id' => null,
        'increment_id' => null,
        'total_qty' => $order->getTotalQtyOrdered(),
        'is_closed' => false,
        'items_to_ship' => $itemsToShip,
        'created_at' => null
    );

    try {
        $shipment = Mage::getModel('sales/order_shipment_api')->create($orderIncrementId, $shipmentData);
    } catch (Exception $e) {
        throw new Exception("Error creating shipment: " . $e->getMessage());
    }

    if ($shipmentTrackingNumber !== null && $shipment !== false) {
        // Add tracking information
        Mage::getModel('sales/order_shipment_track')->addData(
            array(
                'shipment_id' => $shipment->getId(),
                'title'       => 'Standard Delivery',
                'number'      => $shipmentTrackingNumber,
                'carrier_code'   => 'standard_delivery'
            )
        );
    }

    // Save and send shipment email
    $shipment->save();

    $sent = 0;
    if (!is_null($order->getCustomerEmail())) {
        $sent = $shipment->sendEmail(true);
        $shipment->setEmailSent(true);
    }

    return $sent !== false;
}

This script creates a new shipment for the given order ID, checks if there are any items left to ship, and then sends an email with shipment details. It handles completed orders by using Mage::getModel('sales/order_shipment_api') which is specifically designed for creating shipments.

Conclusion

Automating shipment emails for completed orders in Magento requires a careful understanding of order states and the appropriate model to use (Mage_Sales_Model_Order_Shipment). By incorporating these changes, your script should now send email notifications with proper shipment details for all your completed orders, including those that come from CSV imports.

Remember to thoroughly test this script on your development environment before deploying it to production. If you have any further questions or need additional assistance, feel free to reach out!

For more advanced Magento customizations and automation techniques, I highly recommend checking out the expert services available at Laravel Company. They offer top-notch Magento development solutions tailored to your specific business needs.

Happy coding!

Email Orders Errors JavaScript

Our network