使用Zend_Queue將電子郵件放入隊列中進行異步後臺處理。您將需要一個cron作業來在後臺處理隊列。
protected function _enqueueEmail(WikiEmailArticle $email)
{
static $intialized = false;
if (!$initialized) {
$this->_initializeMailQueue("wikiappwork_queue");
$initialized = true;
}
$this->_mailQueue->send(serialize($email));
}
protected function _initializeMailQueue()
{
/* See: 1.) http://framework.zend.com/manual/en/zend.queue.adapters.html and
* 2.) Zend/Queue/Adapter/Db/mysql.sql.
*/
$ini = Zend_Controller_Front::getInstance()->getParam('bootstrap')
->getOptions();
$queueAdapterOptions = array('driverOptions' => array(
'host' => $ini['resources']['multidb']['zqueue']['host'],
'username' => $ini['resources']['multidb']['zqueue']['username'],
'password' => $ini['resources']['multidb']['zqueue']['password'],
'dbname' => $ini['resources']['multidb']['zqueue']['dbname'],
'type' => $ini['resources']['multidb']['zqueue']['adapter']),
'name' => $ini['resources']['multidb']['zqueue']['queueName']);
$this->_mailQueue = new Zend_Queue('Db', $queueAdapterOptions);
}
那麼對於cron作業,像
<?php
use \Wiki\Email\WikiEmailArticle;
// Change this define to correspond to the location of the wikiapp.work/libary
define('APPLICATION_PATH', '/home/kurt/public_html/wikiapp.work/application');
set_include_path(implode(PATH_SEPARATOR, array(
APPLICATION_PATH . '/../library',
get_include_path(),
)));
// autoloader (uses closure) for loading both WikiXXX classes and Zend_ classes.
spl_autoload_register(function ($className) {
// Zend classes need underscore converted to PATH_SEPARATOR
if (strpos($className, 'Zend_') === 0) {
$className = str_replace('_', '/', $className);
}
$file = str_replace('\\', '/', $className . '.php');
// search include path for the file.
$include_dirs = explode(PATH_SEPARATOR, get_include_path());
foreach($include_dirs as $dir) {
$full_file = $dir . '/'. $file;
if (file_exists($full_file)) {
require_once $full_file;
return true;
}
}
return false;
});
// Load and parese ini file, grabing sections we need.
$ini = new Zend_Config_Ini(APPLICATION_PATH .
'/configs/application.ini', 'production');
$queue_config = $ini->resources->multidb->zqueue;
$smtp_config = $ini->email->smtp;
$queueAdapterOptions = array('driverOptions' => array(
'host' => $queue_config->host,
'username' => $queue_config->username,
'password' => $queue_config->password,
'dbname' => $queue_config->dbname,
'type' => $queue_config->adapter),
'name' => $queue_config->queuename);
$queue = new Zend_Queue('Db', $queueAdapterOptions);
$smtp = new Zend_Mail_Transport_Smtp($smtp_config->server, array(
'auth' => $smtp_config->auth,
'username' => $smtp_config->username,
'password' => $smtp_config->password,
'port' => $smtp_config->port,
'ssl' => $smtp_config->ssl
));
Zend_Mail::setDefaultTransport($smtp);
$messages = $queue->receive(10);
foreach($messages as $message) {
// new WikiEmailArticle.
$email = unserialize($message->body);
try {
$email->send();
} catch(Zend_Mail_Exception $e) {
// Log the error?
$msg = $e->getMessage();
$str = $e->__toString();
$trace = preg_replace('/(\d\d?\.)/', '\1\r', $str);
} // end try
$queue->deleteMessage($message);
} // end foreach
這是一個令人困惑的答案,因爲如果不使用cron的腳本,腳本超時時發送的電子郵件將成爲一個問題之前交通的關注。 – rick 2009-04-25 18:42:37
我認爲菲爾建議您使用cron來限制發送電子郵件。例如,每30分鐘一次只發送100個,直到列表用盡。 – grossvogel 2009-04-25 19:05:28
但他似乎建議crontab應該被用作高流量的解決方案?無論如何,我們都應該如此幸運,以便通過營銷活動產生過多的流量。有可能,這不是一個問題。 – rick 2009-04-25 19:10:31