4

I am working on one of E commerce magento site. Where I occur one error. When customer place order they have not receive the order mail. Instead of that order mail goes to previous customer

Ex. If customer place order on date 2 jan 2016, the mail goes to 27 oct 2015 customer

I checked the core_email_queue and core email_queue_recipient tables of Database. In which order get deleted from core_email_queue but same order message_id data present in core_email_queue_recipient table.

Please can any one help me to solve out this query because I confused in core_email_queue functionality and cron job performing functionality with it.

5
  • Do you mean that the mail for the order of the 2 jan 2016 goes to the customer of the order of the 27 oct 2015 (mail is sent to wrong customer) or that the mail for the order of the 27 oct 2015 is sent when a new order is placed (mail is sent correctly but at the following order only) ?
    – hellimac
    Commented Jan 5, 2016 at 3:41
  • yes same thing happened with other customer too
    – Ashwini
    Commented Jan 5, 2016 at 5:00
  • and order contents are ok I mean the ordered product its address everything ok . only mail goes to previous customers.
    – Ashwini
    Commented Jan 5, 2016 at 5:01
  • Same problem here... Did you found the solution? by Barret0
    – Amit Bera
    Commented Jan 28, 2016 at 19:08
  • I followed the Below link:-magento.stackexchange.com/questions/53961/… for my issue. and it get resolved.
    – Ashwini
    Commented Jan 29, 2016 at 4:30

2 Answers 2

15

This issue must be related to the new Magento Email Queue system, that leaves orphan records on the Recipients table. If this is your issue, I send you a fix.

The new Magento Email Queue system manages these two tables: core_email_queue and core_email_queue_recipients. The former one handles the email Messages, and the later one, the Recipients of these messages.

The core_email_queue table is cleaned out as emails on the Magento Email Queue are sent. This cleaning is performed by a cron tab job called core_email_queue_clean_up, that's defined inside the app/code/core/Mage/Core/etc/config.xml config file. The code that performs the cleaning is defined on the removeSentMessages function in the Mage_Core_Model_Resource_Email_Queue class:

/**
 * Remove already sent messages
 *
 * @return Mage_Core_Model_Resource_Email_Queue
 */
public function removeSentMessages()
{
    $this->_getWriteAdapter()->delete($this->getMainTable(), 'processed_at IS NOT NULL');
    return $this;
}

The above code is executed once a day by the cron task.

But it happens that the core_email_queue_recipients table (the one that holds email Recipients, and that is linked to the core_email_queue table by the message_id field ), is not cleaned together with the core_email_queue table (the one that holds email Messages), leaving orphan records inside that Recipients table when then Message table is cleaned out.

The issue described here arises when the core_email_queue table (Messages) is reset and the autoincrement message_id field on this table is reinitiated to 1.

Because the core_email_queue_recipients table (Recipients) has not been cleaned accordingly, when new emails are added to the Magento Email Queue, new records are created on the core_email_queue table (with message_id starting again from 1), and at the same time new records are created on the core_email_queue_recipients table with these same ids (starting again from 1).

The problem is that these ids may already exist on the Recipients table as orphans records (due to previous email messages). These new mesages ids then get repeated inside the core_email_queue_recipients table. In the end, different email Messages are linked to their corresponding Recipients by the message_id, but they get also wrongly linked to previous recepients that were assigned the same message_id from previous emails.

Thus, when recipients are searched to send a given message, besides the appropriate recipient, other wrong recipients may arise.

Fortunately the fix for this issue is easy to perform.

All that's needed is cleaning all repeated messages ids on the core_email_queue_recipients table, and making sure that when a Message is deleted on the core_email_queue table, at the same time its corresponding Recipients get deleted on the core_email_queue_recipients table.

The best way to achieve this is to create a foreign key that links these records and deletes them on cascade (but you need to make some cleaning before you can do that).

For solving the order email issue I followed the below steps:-

1) Execute the following two SQL queries to clean the core_email_queue_recipients table from orphan records and repeated messages ids:

DELETE FROM core_email_queue_recipients WHERE message_id NOT IN (SELECT message_id FROM core_email_queue);
DELETE FROM core_email_queue_recipients WHERE recipient_id < (SELECT recipient_id FROM (SELECT recipient_id FROM core_email_queue_recipients ORDER BY message_id ASC, recipient_id DESC LIMIT 1) AS r);

The first query deletes orphan records, and the second one deletes old records that are no longer valid.

2) Create a foreign key on the core_email_queue_recipients table to delete Recipients records on cascade. The SQL query to create this foreign key is:

ALTER TABLE core_email_queue_recipients ADD FOREIGN KEY(message_id) REFERENCES core_email_queue(message_id) ON DELETE CASCADE;

By using this new foreign key, no orphan records will be left on the core_email_queue_recipients table when cleaning the core_email_queue table, and no duplicated messages to wrong recipients will be sent in the future.

1

If you are using AOE Scheduler then set Cron expression for core_email_queue_clean_up example (*/5 * * * * )

After did this , old sent email data will be clean and all process will be run smoothly.

1
  • Thanks for your solution ,But I used below method to solve my query.
    – Ashwini
    Commented Feb 17, 2016 at 7:11

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