27

I'm using Magento 1.9.1 and when creating a new order, whether it's from the frontend or Admin, the system is sending out 2 emails - both the same!

I've looked on the net and found a bug which was an old one and I couldn't find the code mentioned to comment out, it also looked like that was to do with Paypal orders and this isn't the case.

I've disabled ALL extensions and tried and still had the same issue so I'm confident that it isn't an Extension issue.

4
  • 1
    Have you solved this problem? I got the same strange double order confirmation from time to time and using bcc for confirmatiion mails, too. Don't know whether a customer get also two mails, but I don't hope so.
    – Fox
    Commented May 29, 2015 at 8:25
  • Who is the email addressed to? Are you checking out with one of your admin emails?
    – SR_Magento
    Commented Oct 22, 2015 at 15:56
  • I am getting the same issue (user is receiving two emails after placing new order). I am not sure for below Answer and my both tables "core_email_queue" and "core_email_queue_recipients" are empty, so I think this should be not my case. Please help me on this. Commented Mar 26, 2019 at 6:18
  • Abi Sharma, I have the same issue, still, I get duplicate emails... did you solve the issue? please let me know
    – Lior Loria
    Commented Sep 4, 2019 at 15:09

3 Answers 3

48

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).

This is the procedure to fix the issue:

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.

4
  • 2
    Great research and provided working fix César. This is a major oversight by the Magento team given that this will potentially send customer's name and address to other customers. Anyone know if this has been addressed in a (which) patch/release at all since this problem was discovered?
    – zigojacko
    Commented Mar 31, 2016 at 10:50
  • Still facing the issue of same Order email being sent multiple times to same email address after implementing above solution.Could you please help ?? Commented Sep 6, 2016 at 8:18
  • I am checking my both tables are already empty, so Do I have to run these two steps (queries) ??? Commented Mar 26, 2019 at 7:12
  • Any how I have performed these queries but no help.. Still getting order email twice Commented Mar 26, 2019 at 9:24
6

I had the same problem. For every order I'd receive the customers e-mail and a separate e-mail to my store address - both into the mailbox of my store e-mail account.

In Magento Admin: System > Configuration > Sales E-Mails > Order

I've set the e-mail "Send Order E-Mail Copy Method" from "Seperate E-Mail" to "Bcc", and it works now. I'm only receiving one e-mail per order now.

2
  • 3
    I'm already using the Bcc method so that isn't the issue for me unfortunately :( Commented Feb 17, 2015 at 15:59
  • I am also using this setting already, but user is getting 2 emails after placing new order. Commented Mar 26, 2019 at 6:27
1

I had the same problem. In my case the problem was caused by the source code of the cron.sh script.

My hosting provider used his own versions of the cron.sh and cron.php scripts for the Magento cron-job.

When i changed from the cron.sh script to the cron.php script for starting the Magento-Cron-Job the new order eMail was only sent once, my probelm was solved.

Maybe your issue has something to do with the code of the cron.sh or cron.php script.

1
  • Hi, We are already using cron.php script for Magento cron job but I am getting emails twice. Commented Mar 26, 2019 at 9:39

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