3

I am sent an email when an order is placed, but there is speed issue. Can you advise me how to add a custom email to the queue when an order is placed?

try {
        $mail = Mage::getModel('core/email');
        $mail->setToName($email);
        $mail->setToEmail($email);
        $mail->setBody($html);
        $mail->setSubject("Automated Bundle Purchase: Coupon Code");
        $mail->setFromEmail(Mage::getStoreConfig('trans_email/ident_general/email'));
        $mail->setFromName(Mage::getStoreConfig('trans_email/ident_general/name'));
        $mail->setType('html');
        $mail->send();
    } catch (Exception $e) { 
        Mage::log($e->getMessage());
    }
2

When the message is queued, the email data is saved in the Magento database where it will be later fetched and an email message will be reconstructed from the data and sent. The data is stored in two new tables, one for the email data and one for the recipients:

<email_queue>
    <table>core_email_queue</table>
</email_queue>
<email_recipients>
    <table>core_email_queue_recipients</table>
</email_recipients>

When and how is the queue cleared ?

<core_email_queue_send_all>
    <schedule><cron_expr>*/1 * * * *</cron_expr></schedule>
    <run><model>core/email_queue::send</model></run>
</core_email_queue_send_all>
<core_email_queue_clean_up>
    <schedule><cron_expr>0 0 * * *</cron_expr></schedule>
    <run><model>core/email_queue::cleanQueue</model></run>
</core_email_queue_clean_up>

That means the send() function on the queue will be called every minute and the clean up once per day at midnight 00:00.

So you can reference you to this one :

if ($this->hasQueue() && $this->getQueue() instanceof Mage_Core_Model_Email_Queue) {
    /** @var $emailQueue Mage_Core_Model_Email_Queue */
    $emailQueue = $this->getQueue();
    $emailQueue->setMessageBody($text);
    $emailQueue->setMessageParameters(array(
            'subject'           => $subject,
            'return_path_email' => $returnPathEmail,
            'is_plain'          => $this->isPlain(),
            'from_email'        => $this->getSenderEmail(),
            'from_name'         => $this->getSenderName(),
            'reply_to'          => $this->getMail()->getReplyTo(),
            'return_to'         => $this->getMail()->getReturnPath(),
        ))
        ->addRecipients($emails, $names, Mage_Core_Model_Email_Queue::EMAIL_TYPE_TO)
        ->addRecipients($this->_bccEmails, array(), Mage_Core_Model_Email_Queue::EMAIL_TYPE_BCC);
    $emailQueue->addMessageToQueue();

    return true;
}

Reference

EDIT: Try to add this before send():

/** @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);

        $mail->setQueue($emailQueue)->send();
2
  • This all is accepted. But how we can add custom email on that queue table. You sent me mechanism. But how we can add an entry of mail in that queue table? – Pratik Kamani Jul 20 '17 at 9:44
  • You create your custom module custom email based on Mage_Core_Model_Email_Template => sendTransactional, Mage_Core_Model_Email_Queue I don't see what is complicated. Look my Edit – PЯINCƏ Jul 20 '17 at 10:17

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.