問題描述
我正在嘗試為我的應(yīng)用程序?qū)崿F(xiàn)一個郵件列表系統(tǒng).我目前使用 Zend_Mail_Transport_Smtp('localhost')
作為我的傳輸,循環(huán)瀏覽我的訂閱者列表,并向每個訂閱者發(fā)送一個新的 Zend_Mail
.但是,我注意到腳本完成所需的時間隨著訂閱者數(shù)量的增加而增加.
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.
我相信一定有更專業(yè)的方法來做到這一點(diǎn),包括電子郵件排隊(duì).我想最理想的方法是讓用戶填寫表單,點(diǎn)擊發(fā)送,然后立即得到回復(fù)說正在發(fā)送電子郵件,而不是等待數(shù)百封電子郵件發(fā)送完畢.
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
不做任何排序郵件排隊(duì).任何有這方面經(jīng)驗(yàn)的人都可以給我一個關(guān)于如何做到這一點(diǎn)的概述嗎?我對 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 可靠地發(fā)送大量電子郵件,您必須使用排隊(duì)機(jī)制.正如其他人所建議的,使用隊(duì)列的過程如下所示:
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:
- 遍歷您的一組用戶,為每個用戶創(chuàng)建電子郵件并可能自定義內(nèi)容
- 將每個郵件對象傳遞到隊(duì)列,這將延遲發(fā)送電子郵件
- 在某種 cron 腳本中,一次發(fā)送數(shù)百個隊(duì)列內(nèi)容.注意:您需要通過查看日志以查看實(shí)際發(fā)送過程中返回的錯誤來調(diào)整發(fā)送的電子郵件數(shù)量.如果您嘗試發(fā)送太多郵件,我已經(jīng)注意到郵件傳輸將不再接受連接(我使用的是 qmail)
您可以使用一些庫來執(zhí)行此操作,PEAR 郵件隊(duì)列(使用 Mail_Mime)和 SwiftMailer 都允許您創(chuàng)建和排隊(duì)電子郵件.到目前為止,Zend Mail 只提供電子郵件的創(chuàng)建,不提供排隊(duì)(稍后會詳細(xì)介紹).
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 郵件隊(duì)列 的經(jīng)驗(yàn),但有一些問題.如果您嘗試將大量電子郵件排隊(duì)(例如,循環(huán)超過 20,000 個用戶并嘗試在合理的時間內(nèi)將他們放入隊(duì)列),則使用 Mail Mime 的帶引號的可打印編碼實(shí)現(xiàn)非常慢.您可以通過切換到 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 郵件隊(duì)列.我已經(jīng)取得了一些成功,但需要花點(diǎn)時間才能做到正確.為此,請擴(kuò)展 Zend Mail Transport Abstract,實(shí)現(xiàn) _sendMail 方法(您將在該方法中將 Zend Mail 對象放入郵件隊(duì)列)并將傳輸對象的實(shí)例傳遞給 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().
最重要的是,您可以通過多種方式做到這一點(diǎn),但這需要您進(jìn)行一些研究和學(xué)習(xí).然而,這是一個非常容易解決的問題.
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.
這篇關(guān)于從 Zend Framework 應(yīng)用程序向數(shù)百個收件人發(fā)送電子郵件的最佳方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!