/php/cli/mqueue.php
PHP | 130 lines | 69 code | 34 blank | 27 comment | 10 complexity | 6f7a45a47c9cd0138208ba14e7a79857 MD5 | raw file
- #!/usr/bin/php
- <?php
- //script to process mailer queue
-
- require_once ('gloo.inc' );
- require_once ($_SERVER['GLOO_INC_DIR'].'class_loader.inc' );
- function mailer_error_handler($errorno,$errorstr,$file,$line) {
- switch($errorno) {
- case E_STRICT :
- //do nothing
- // PEAR packages use PHP-4 compatible code that throws
- // errors in E_STRICT mode
- Gloo_Logger::getInstance()->error($errorstr);
- return true;
- default:
- Gloo_Logger::getInstance()->error($errorstr);
- }
-
- exit ;
- }
-
- function sendMail($row,$farmName) {
- $flag = false ;
- $smtp = Gloo_Mail::getInstance();
- $subject = $row['subject'] ;
- $text = $row['body_text'];
- $html = $row['body_html'];
- $toAddress = $row['to_address'];
- $fromAddress = $row['from_address'];
-
-
- if($row['is_nam'] == 1 ) {
- //change subject and body for new account mails
- // we need domain | email | password
- // email is toAddress
- // domain is subject
- // password is body_text
-
- $text = Gloo_UI_Html::newAccountTextMail($row['subject'], $toAddress, $row['body_text']);
- $html = Gloo_UI_Html::newAccountHtmlMail($row['subject'], $toAddress, $row['body_text']);
- $subject = $farmName . ' - New account details';
-
- }
-
-
- $smtp->sendHtmlMail($subject,$text,$html ,$toAddress, $fromAddress);
-
- if($smtp->isError()) {
- Gloo_Logger::getInstance()->error($smtp->getMessage());
- } else {
- $flag = true ;
- }
- return $flag ;
- }
- //start script
- // PEAR MAIL will not work with our error handler
- // there are many issues with using PEAR mail with PHP 5.3
- // turn On this error handler for debugging other part of script
- //set_error_handler('mailer_error_handler');
- ob_start();
-
- //variables initialization
- //time in seconds since UNIX epoch
- $startTime = time();
- //Run the script for 10 mins max.
- $timeDelta = 600 ;
- $errorDelta = 3 ;
- $glooConfig = Gloo_Config::getInstance();
- $farmName = $glooConfig->getFarmName();
-
- //check if we have an SMTP server
- // bail out if no SMTP server defined
- if(!$glooConfig->hasSmtpServer()){
- trigger_error('No smtp server set in config file',E_USER_ERROR);
- exit ;
- }
-
-
-
- //Fetch the mail queue
- $mailerDao = new Gloo_Dao_Mailer();
- $total = $mailerDao->getRecordsCount();
- $pageSize = 20 ;
- $page = 1 ;
- $paginator = new Gloo_UI_Paginator($page, $total, $pageSize);
- $totalPages = $paginator->getTotalPages() ;
- //echo " Mail Queue :: $total mails to process!" ;
- $numberOfErrors = 0 ;
-
-
-
- for($page = 1 ; $page <= $totalPages ; $page++ ) {
- //fetch 20 at a time
- $rows = $mailerDao->getRecords($paginator);
-
- //send mails
- foreach($rows as $row) {
- //time for break?
- if( ((time() - $startTime) > $timeDelta) || ($numberOfErrors > $errorDelta) ) {
- $message = "Mail queue processing halted :: time or errors delta achieved" ;
- Gloo_Logger::getInstance()->error($message);
- //break 2; /* exit both rows loop and pages loop */
- exit;
- }
-
- $flag = sendMail($row,$farmName);
- if($flag){
- $mailerDao->markAsSent($row['id']);
- } else {
- //wait a bit - 10 seconds
- $numberOfErrors++ ;
- sleep(10);
-
- }
-
- }
- }
-
- ?>