Magento1.9.1.0重要改进之一--邮件异步队列发送

In this post I wanted to take a tour through the new functionality in Magento 1.9.1 with regards to sending emails from your store.

The astute among you will have noticed there were some changes to email in Magento 1.9.1 from the Magento
1.9.1 release notes
. Two things jump out as requiring further investigation:

  1. “all Magento e-mails (including order confirmation and transactional) are now queued and sent according to your configured cron schedule”
  2. “boasts responsive default email templates so customers can read your order confirmation emails and newsletters on any device”

The first change sounds worrying to me, after years of emails from merchants struggling to get their Magento store to send emails – anything which makes it _harder_ seems like a bad idea to me. However, we’ll look through
the changes and see what’s new.

The second change sounds really good – I was tinkering on a responsive email extension myself, so it’s pleasing to see it has already become core functionality. I’ll take a look through the template changes, and how they affect your store in the second part
of this blog post series.

Using cron to send Magento Emails

Magento email sending all boils down to the Template class Mage_Core_Model_Email_Template,
you can see below the change for queuing has been to introduce a check for an available queue in the template class, and if available to enqueue the message with all of it’s data and return immediately.

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;
}

If there is no queue, the send() function
proceeds as it used to, builds the mail object and sends it. Extensions like SMTP
Pro
 and MageSend that hook in to the Template class to change the mail transport,
will inject their transport at this point.

Slight Tangent: Are all emails actually being queued?
You might be thinking, as I was at this point, Under what circumstances will there be no queue?Good question. According to the
release notes
, “all Magento e-mails” are now queued, so I expect that the queue must be being set somewhere for all outbound emails. Let’s dig into this and find out.

Firstly, where the queue object is instantiated with a getModel() or getSingleton() call
it would be referenced by it’s handle core/email_queue.
So we grep for that handle, to find the places where it is instantiated. Oddly, I only find two places where the queue is created and set on the Template object. Both are in Mage_Sales_Model_Order,
functions queueNewOrderEmail()and queueOrderUpdateEmail().
So my gut feeling at this stage is I have missed some obvious place where the queue is being set for *all* other emails (ignoring the obvious question, if it’s being set for all emails, why set it explicitly here?).

So, let’s try and find calls to the setQueue() method,
assuming the core developers have not used a setData() – they must have called setQueue somewhere, to ensure the queue object is being used. Strangely I only find reference to Template.php and Mailer.php, both setting the queue based on what was passed to
it.

OK, so I’m not sure all emails are being queued, let’s ask
the community to double check me
 and continue with our analysis.

When Emails are queued…
If the message is queued, then 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? For that Magento have added two new cron jobs:

<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 (why */1 and
not just* I wonder?) and the clean up once
per day at midnight 00:00.

The send() function will grab a chunk of
size MESSAGES_LIMIT_PER_CRON_RUN (default
100) emails data out of the DB table and send the emails one by one that have been queued since the last run. The send itself is handled in the same way the Template class
does in the pre 1.9.1 code.

$collection = Mage::getModel('core/email_queue')->getCollection()
    ->addOnlyForSendingFilter()
    ->setPageSize(self::MESSAGES_LIMIT_PER_CRON_RUN)
    ->setCurPage(1)
    ->load();

A couple of things I wish the core team had done differently here. 1) to allow configuration of the rate of email sending from either the XML file or via the admin panel. It’s important for most 3rd party email sending
services to control the rate of email sending to avoid being flagged as spam.

And 2) to re-use the email sending functionality from the Template class – instead of basically copy-pasting the Template class sending code in the Queue. This has the effect that extensions like SMTP Pro or MageSend
– or any that try to modify the passage of outbound emails – will now need an extra override, one that is totally unrelated to actual sending, just to set the transport before sending.

Lastly, 3) the email queue table could also act as an email log (if all emails are sent using it) and even allow users to easily resend the emails. All of these are items I
will try to address as 3rd party functionality in the next releases of SMTP Pro and MageSend.

I also think it’s important to test whether cron is running as part of the self test/setup – as it’s one of the areas of Magento that often is not handled correctly. I wonder why Magento just doesn’t do an auto-cron on front end page views like WordPress, but
that can be a post for another time.

I hope this little tour through the new functionality helps you to understand the new email sending queue functionality in Magento 1.9.1. In the next post in this series I’ll take an in-depth look at the new responsive email templates shipped as part of Magento
1.9.1 – how they work, how you can customize them and things I’d like to see improved/changed (if any).

原文:Sending Emails by Queue in
Magento 1.9.1

时间: 2025-01-21 11:49:53

Magento1.9.1.0重要改进之一--邮件异步队列发送的相关文章

ASP 3.0对ASP 2.0的改进

 ASP 3.0对ASP 2.0的改进 下面的一些特征是从2.0版本中改进或升级来的. 1. 缓冲缺省为打开状态ASP提供可选的输出缓冲.从IIS 4.0开始,这使得脚本执行得更快,并提供对流向浏览器的输出的控制能力.在ASP 3.0这个改进的性能通过改变Reponse.Buffer属性的缺省设置为True而反映出来.缺省状态下缓冲是打开的,这意味着最终输出只有在进程完成时,或脚本调用Response.Flush或Response.End方法时,才送至客户端.注意,可以通过设置Response.

新推出的金融版eXtremeDB 6.0功能改进预览_数据库其它

通过SQL和Python访问基于矢量的函数来提高编程速度和生产力.分布式查询处理和运用RLE压缩来处理资本市场中的大数据分析. 华盛顿州联邦路,2014年10月27日–McObject宣布eXtremeDB金融版本6.0已正式发布,这也是面向资本市场数据库系统技术的重要升级.此次改进包括使用广泛应用的SQL和Python语言来访问eXtremeDB金融版最强大的功能,用陈述性的SQL语言以及快速原型法的Python语言进行开发释放生产力.6.0版本运用其崭新的功能:分布式查询处理和专业的市场数据

邮件例程 - JavaMail - 发送HTML邮件

form.htm========<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>邮件例程 - JavaMail - 发送HTML邮件</title></head><body><table border="0" cellspacing=

PostgreSQL 10.0 preview sharding增强 - 支持异步, 多节点并行

标签 PostgreSQL , 10.0 , sharding 增强 背景 PostgreSQL 10.0的sharding增强,比如聚合函数的下推算一个. 现在又带来了一个核武器级别的增强,postgres_fdw的异步化调用. 为什么说是核武器级别的呢,比如你有16个sharding节点(对应postgres_fdw的16个库),10.0以前,如果我需要执行一个QUERY涉及到所有的sharding节点时,每个sharding节点是串行执行的然后append结果. 10.0开始,会变成异步化

基于redis的邮件异步发送

基于redis的邮件异步发送 相关工具和内容 安装Redis服务 编写Yii2的插件类 重写SwiftMailer类的方法 提交compsoer 提交至github 提交至composer 测试使用composer下载 安装redis服务 redis官网reids.io 下载地址 当前稳定版本链接http://download.redis.io/releases/redis-4.0.1.tar.gz mkdir redis cd redis wget http://download.redis.

phpmailer发邮件,显示发送成功,邮件却没收到

问题描述 phpmailer发邮件,显示发送成功,邮件却没收到 这是配置信息 这是提示信息 Invalid address是哪儿来的?为什么已经显示发送成功却收不到邮件? 解决方案 通过phpmailer使用gmail账号发送邮件了[转]用 phpmailer 发送邮件PHPmailer发送邮件 解决方案二: 163的邮箱里需要配置一下的

asp.net页面作为邮件正文进行发送

问题描述 动态的asp.net页面怎么作为邮件内容进行发送 解决方案 解决方案二:Refer:解决方案三:stringstrDomainPath=HttpRuntime.AppDomainAppPath;stringstrHtmlPath=strDomainPath+"index.htm";stringstrUrl="http://www.1.com/Index.aspx";System.Net.WebRequestwReq=System.Net.WebReques

看到有时邮箱收到广告邮件,打开邮件头发现,发送都和接收都邮箱地址都一样,这是怎么实现的.

问题描述 看到有时邮箱收到广告邮件,打开邮件头发现,发送都和接收都邮箱地址都一样,而且都是匿名的,这是怎么实现的? 解决方案 解决方案二:帮忙顶一下!!解决方案三:有些可以匿名發送郵件的郵局發件地址可以隨便寫的,或者自己建個郵局解决方案四:如果用自己本机作为smtp服务器,发送邮件的地址你填谁的都行,接受邮件的服务器大多只能检查本域名的邮件地址解决方案五:接收人的地址为什么也可以改呀,我收到一个广告邮件,发送者和接收者的地址是一样的,怪了解决方案六:就是,接收者,显示发送者的名称,查了一下邮件头

Manitou-Mail 1.2.0发布 数据库驱动邮件工具

Manitou-Mail是一个数据库驱动的电子邮件工具.通过使用PostgreSQL数据库来存储引擎,具有一个Perl守护进程用来邮件的发送/接收和17813.html">可定制的分析/索引,类似于邮件程序的用户界面(C++++/Qt)连接到数据库.提供了一个数据库风格的架构来处理电子邮件.其特点是快速.共享访问.微调安全.稳健的存储功能.一致的备份.审计.统计和可定制的附加组件. Manitou-Mail 1.2.0该版本的过滤系统功能已改进,并重新设计编辑器.过滤表达式现在可以在用户界