8

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!

11

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();
  • Thank you a lot for the suggestion! I actually did the same, but the other way around. I added the invoice to the queue instead. It worked, but I wonder if it's also right to do? – Pat Feb 24 '16 at 11:01
  • 1
    Yes, sure. I'll add to my post for other users. – Phoenix128_RiccardoT Feb 24 '16 at 11:02
  • 1
    Seems to good solution. Will give a try – Jai Mar 16 '16 at 18:09
  • 1
    I used solution 1 and it worked form me. Thanks – Jai Mar 17 '16 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.