In Magento 1.x if you set this config to enabled Store > Configration > Sales > Sales Emails > General Settings > Asynchronous sending
, Magento, instead of sending the email when the order is placed, adds an entry in a queue table (core_email_queue
), and then a cron job comes, reads all the entries from the queue and sends the emails.
I don't find core_email_queue
table in Magento 2. How does this feature work in Magento 2?
Best Answer
I had no idea how emails are sent in Magento 2, but your question took my interest so I investigated a little.
I have started my investigation by searching email keyword on cronjobs, and got the following output.
So, there are some cronjobs which are responsible for sending emails. This shows us that they are async.
I have selected
sales_send_order_emails
cronjob to investigate because I can easily create a new order to test it. Then I have searched forsales_send_order_emails
in the whole project, and found the following filevendor/magento/module-sales/etc/crontab.xml
. It has the following cron-job entry.SalesOrderSendEmailsCron
is clearly a virtual type otherwise it should have a namespaces. This virtual type is defined invendor/magento/module-sales/etc/di.xml
.So the virtual type
SalesOrderSendEmailsCron
is actually the classMagento\Sales\Cron\SendEmails
. As you can see this class has a constructor with only one argument, and it is given asSalesOrderSendEmails
virtual type. This virtual type is also defined in thevendor/magento/module-sales/etc/di.xml
. Here is the type definition:So the real mail sender is
Magento\Sales\Model\EmailSenderHandler
. If you open this file, you will see the following method:If you set
sales_email/general/async_sending
config astrue
, then this cronjob will be sending mails. It also has the following filters to fetch orders.As a result, you have
send_email
andemail_sent
attributes in yoursales_order
table. Ifsend_email
is set to1
for an order, this cronjob should send an email for the order. Ifemail_sent
is notNULL
, then it has already done.