Question

In Magento 1.9, why are order emails send through the queue, while Invoice emails are sent directly? I checked the code for Order and Invoice, and the order-sendNewOrderEmail uses the queue, while invoice->sendEmail() skips it completely. It's making the order email be sent after the invoice email instead of before in case the order is approved automatically!

Was it helpful?

Solution

Orders email are sent using the queue for different reasons:

  • Avoid frontend slowdown while sending out the email
  • Resend on failure
  • Avoid errors on checkout/post-checkout

Solution 1 (send order's email immediately):

If you like to send order email immediately you can consider overriding the Mage_Sales_Model_Order::queueNewOrderEmail() method by chainging the following lines:

/** @var $emailQueue Mage_Core_Model_Email_Queue */
$emailQueue = Mage::getModel('core/email_queue');
$emailQueue->setEntityId($this->getId())
    ->setEntityType(self::ENTITY)
    ->setEventType(self::EMAIL_EVENT_NAME_NEW_ORDER)
    ->setIsForceCheck(!$forceMode);

$mailer->setQueue($emailQueue)->send();

to:

    /** @var $emailQueue Mage_Core_Model_Email_Queue */
    $mailer->send();

Solution 2 (send invoices using queue):

The opposite solution is to let invoices use the queue:

You must override Mage_Sales_Model_Order_Invoice::sendEmail changing:

// Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId($templateId);
        $mailer->setTemplateParams(array(
                'order'        => $order,
                'invoice'      => $this,
                'comment'      => $comment,
                'billing'      => $order->getBillingAddress(),
                'payment_html' => $paymentBlockHtml
            )
        );
        $mailer->send();

To:

// Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId($templateId);
        $mailer->setTemplateParams(array(
                'order'        => $order,
                'invoice'      => $this,
                'comment'      => $comment,
                'billing'      => $order->getBillingAddress(),
                'payment_html' => $paymentBlockHtml
            )
        );

        $emailQueue = Mage::getModel('core/email_queue');
        $emailQueue->setEntityId($this->getId())
          ->setEntityType('order_invoice')
          ->setEventType('new_invoice');

        $mailer->setQueue($emailQueue)->send();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top