create invoice and email in Magento

create invoice and email in Magento

October 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
create invoice and email in Magento

Automating Invoice Creation and Email Delivery in Magento

As an e-commerce developer, you understand the importance of automating processes like invoice generation and email delivery to streamline operations. In this post, we’ll walk through troubleshooting an issue with email functionality after successfully creating invoices in Magento.

Understanding the Invoice Generation Process

The provided code demonstrates a typical approach to generating invoices programmatically:

Mage::app();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderid);

try {
    if (!$order->canInvoice()) {
        Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
    }

    $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

    if (!$invoice->getTotalQty()) {
        Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
    }

    $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
    $invoice->register();
    $transactionSave = Mage::getModel('core/resource_transaction')
        ->addObject($invoice)
        ->addObject($invoice->getOrder());
    $transactionSave->save();
} catch (Mage_Core_Exception $e) {
}

This script loads an order by increment ID, prepares the invoice using prepareInvoice(), registers it, and saves the transaction. The try-catch block handles any exceptions that might occur during this process.

Troubleshooting Email Delivery Issues

The email delivery issue likely stems from a misconfiguration or a missing trigger somewhere in your Magento setup. Here are some common culprits to investigate:

1. SMTP Configuration

Ensure you have correctly configured the SMTP settings for your store. You can check these under System > Configuration > Sales Emails > Email Configuration.

2. Transactional Emails Not Enabled

Verify that transactional emails are enabled in your Magento backend. Go to Stores > Settings > Configuration > Sales Emails > General Options and ensure “Disable transactional emails” is set to No.

3. Magento Cron Jobs

Magento relies on cron jobs for scheduling email delivery after invoice creation or order placement. Check if the cron jobs (Magento_QuoteImport, Magento_SalesOrderSaveAfter, etc.) are running according to your server’s cron settings.

Best Practices for Automating Processes in Magento

To ensure smooth operation of automated processes like this:

  1. Use Magento’s built-in models and helpers where possible to leverage its core functionality.
  2. Thoroughly test scripts on staging environments before deploying them to production.
  3. Monitor log files (var/log/system.log) for any errors or warnings related to your scripts.

Conclusion

Automating invoice creation and email delivery are powerful features in Magento that can save time and reduce human error. If you’re experiencing email delivery issues after successful invoice generation, carefully review the common troubleshooting points mentioned above.

Remember, Magento provides extensive documentation on its official website: https://laravelcompany.com/magento-documentation

By following best practices and keeping your Magento version up-to-date, you can minimize potential issues with these automated processes.

Catalog Email Orders Import/Export

Our network