PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/php/cli/mqueue.php

http://webgloo.googlecode.com/
PHP | 130 lines | 69 code | 34 blank | 27 comment | 10 complexity | 6f7a45a47c9cd0138208ba14e7a79857 MD5 | raw file
  1. #!/usr/bin/php
  2. <?php
  3. //script to process mailer queue
  4. require_once ('gloo.inc' );
  5. require_once ($_SERVER['GLOO_INC_DIR'].'class_loader.inc' );
  6. function mailer_error_handler($errorno,$errorstr,$file,$line) {
  7. switch($errorno) {
  8. case E_STRICT :
  9. //do nothing
  10. // PEAR packages use PHP-4 compatible code that throws
  11. // errors in E_STRICT mode
  12. Gloo_Logger::getInstance()->error($errorstr);
  13. return true;
  14. default:
  15. Gloo_Logger::getInstance()->error($errorstr);
  16. }
  17. exit ;
  18. }
  19. function sendMail($row,$farmName) {
  20. $flag = false ;
  21. $smtp = Gloo_Mail::getInstance();
  22. $subject = $row['subject'] ;
  23. $text = $row['body_text'];
  24. $html = $row['body_html'];
  25. $toAddress = $row['to_address'];
  26. $fromAddress = $row['from_address'];
  27. if($row['is_nam'] == 1 ) {
  28. //change subject and body for new account mails
  29. // we need domain | email | password
  30. // email is toAddress
  31. // domain is subject
  32. // password is body_text
  33. $text = Gloo_UI_Html::newAccountTextMail($row['subject'], $toAddress, $row['body_text']);
  34. $html = Gloo_UI_Html::newAccountHtmlMail($row['subject'], $toAddress, $row['body_text']);
  35. $subject = $farmName . ' - New account details';
  36. }
  37. $smtp->sendHtmlMail($subject,$text,$html ,$toAddress, $fromAddress);
  38. if($smtp->isError()) {
  39. Gloo_Logger::getInstance()->error($smtp->getMessage());
  40. } else {
  41. $flag = true ;
  42. }
  43. return $flag ;
  44. }
  45. //start script
  46. // PEAR MAIL will not work with our error handler
  47. // there are many issues with using PEAR mail with PHP 5.3
  48. // turn On this error handler for debugging other part of script
  49. //set_error_handler('mailer_error_handler');
  50. ob_start();
  51. //variables initialization
  52. //time in seconds since UNIX epoch
  53. $startTime = time();
  54. //Run the script for 10 mins max.
  55. $timeDelta = 600 ;
  56. $errorDelta = 3 ;
  57. $glooConfig = Gloo_Config::getInstance();
  58. $farmName = $glooConfig->getFarmName();
  59. //check if we have an SMTP server
  60. // bail out if no SMTP server defined
  61. if(!$glooConfig->hasSmtpServer()){
  62. trigger_error('No smtp server set in config file',E_USER_ERROR);
  63. exit ;
  64. }
  65. //Fetch the mail queue
  66. $mailerDao = new Gloo_Dao_Mailer();
  67. $total = $mailerDao->getRecordsCount();
  68. $pageSize = 20 ;
  69. $page = 1 ;
  70. $paginator = new Gloo_UI_Paginator($page, $total, $pageSize);
  71. $totalPages = $paginator->getTotalPages() ;
  72. //echo " Mail Queue :: $total mails to process!" ;
  73. $numberOfErrors = 0 ;
  74. for($page = 1 ; $page <= $totalPages ; $page++ ) {
  75. //fetch 20 at a time
  76. $rows = $mailerDao->getRecords($paginator);
  77. //send mails
  78. foreach($rows as $row) {
  79. //time for break?
  80. if( ((time() - $startTime) > $timeDelta) || ($numberOfErrors > $errorDelta) ) {
  81. $message = "Mail queue processing halted :: time or errors delta achieved" ;
  82. Gloo_Logger::getInstance()->error($message);
  83. //break 2; /* exit both rows loop and pages loop */
  84. exit;
  85. }
  86. $flag = sendMail($row,$farmName);
  87. if($flag){
  88. $mailerDao->markAsSent($row['id']);
  89. } else {
  90. //wait a bit - 10 seconds
  91. $numberOfErrors++ ;
  92. sleep(10);
  93. }
  94. }
  95. }
  96. ?>