从 Zend Framework 应用程序向数百个收件人发送电子邮件的最佳方法是什么?
我正在尝试为我的应用程序实现一个邮件列表系统.我目前使用 Zend_Mail_Transport_Smtp('localhost')
作为我的传输,循环浏览我的订阅者列表,并向每个订阅者发送一个新的 Zend_Mail
.但是,我注意到脚本完成所需的时间随着订阅者数量的增加而增加.
I'm trying to implement a mailing list system for my application. I'm currently using Zend_Mail_Transport_Smtp('localhost')
as my transport, looping through my list of subscribers, and sending a new Zend_Mail
to each one. However, I am noticing that the length of time that it takes for the script to complete increases as the number of subscribers increase.
我相信一定有更专业的方法来做到这一点,包括电子邮件排队.我想最理想的方法是让用户填写表单,点击发送,然后立即得到回复说正在发送电子邮件,而不是等待数百封电子邮件发送完毕.
I'm sure there must be a more professional approach to doing this, involving the queuing of emails. I suppose the ideal approach would be for the user to fill out the form, click send, and immediately get a response saying that the emails are being sent, rather than waiting for the hundreds of emails to finish sending.
我知道 Zend_Mail
不做任何排序邮件排队.任何有这方面经验的人都可以给我一个关于如何做到这一点的概述吗?我对 cron/crontab/cronjobs 一无所知,所以如果涉及到,请解释一下过程.
I understand that Zend_Mail
does not do any sort mail queuing. Could anyone who has experience with this, give me an overview of how this can be done? I don't know anything about cron/crontab/cronjobs, so if it involves that, please explain the process.
推荐答案
为了使用 PHP 可靠地发送大量电子邮件,您必须使用排队机制.正如其他人所建议的,使用队列的过程如下所示:
In order to reliably send a large number of emails using PHP you have to use a queueing mechanism. As suggested by others, the process of using a queue looks something like this:
- 遍历您的一组用户,为每个用户创建电子邮件并可能自定义内容
- 将每个邮件对象传递到队列,这将延迟发送电子邮件
- 在某种 cron 脚本中,一次发送数百个队列内容.注意:您需要通过查看日志以查看实际发送过程中返回的错误来调整发送的电子邮件数量.如果您尝试发送太多邮件,我已经注意到邮件传输将不再接受连接(我使用的是 qmail)
您可以使用一些库来执行此操作,PEAR 邮件队列(使用 Mail_Mime)和 SwiftMailer 都允许您创建和排队电子邮件.到目前为止,Zend Mail 只提供电子邮件的创建,不提供排队(稍后会详细介绍).
There are a few libraries out there you can use to do this, PEAR Mail Queue (with Mail_Mime) and SwiftMailer both allow you to create and queue emails. So far, Zend Mail only provides for creation of emails, not queueing (more on that later).
我主要有使用 PEAR 邮件队列 的经验,但有一些问题.如果您尝试将大量电子邮件排队(例如,循环超过 20,000 个用户并尝试在合理的时间内将他们放入队列),则使用 Mail Mime 的带引号的可打印编码实现非常慢.您可以通过切换到 base64 编码来加快速度.
I have experience primarily with PEAR Mail Queue and there are a few gotchas. If you are trying to queue up a large number of emails (for instance, looping over 20,000 users and trying to get them into the queue in a reasonable time), using Mail Mime's quoted-printable encoding implementation is very slow. You can speed this up by switching to base64 encoding.
对于 Zend Mail,您可以编写一个 Zend Mail Transport 对象,将您的 Zend Mail 对象放入 PEAR 邮件队列.我已经取得了一些成功,但需要花点时间才能做到正确.为此,请扩展 Zend Mail Transport Abstract,实现 _sendMail 方法(您将在该方法中将 Zend Mail 对象放入邮件队列)并将传输对象的实例传递给 Zend Mail 对象的 send() 方法或通过 Zend Mail::setDefaultTransport().
As for Zend Mail, you can write a Zend Mail Transport object that puts your Zend Mail objects into the PEAR Mail Queue. I have done this with some success, but it takes a bit of playing to get it right. To do this, extend Zend Mail Transport Abstract, implement the _sendMail method (which is where you will drop your Zend Mail object into the Mail Queue) and pass the instance of your transport object to the send() method of your Zend Mail object or by Zend Mail::setDefaultTransport().
最重要的是,您可以通过多种方式做到这一点,但这需要您进行一些研究和学习.然而,这是一个非常容易解决的问题.
Bottom line is that there are many ways you can do this, but it will take some research and learning on your behalf. It is a very solvable problem, however.
相关文章