Asychronous arka plan işleme için kuyrukta e-postaları yerleştirmek için Zend_Queue kullanın. Sen arka planda süreçleri kuyruğunu bir cron işi gerekir.
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);
}
Sonra cron iş için, gibi bir komut
<?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