Example #1
0
 /**
  * Sends the queued up messages to their destinations. This can either try
  * to send emails that could not be sent before (status = 'error'), or just
  * emails just recently queued (status = 'pending').
  *
  * @param   string $status The status of the messages that need to be sent
  * @param   integer $limit The limit of emails that we should send at one time
  * @param   boolean $merge Whether or not to send one merged email for multiple entries with the same status and type.
  */
 public static function send($status, $limit = null, $merge = false)
 {
     if ($merge !== false) {
         foreach (self::_getMergedList($status, $limit) as $maq_ids) {
             $emails = self::_getEntries($maq_ids);
             $recipients = array();
             foreach ($emails as $email) {
                 $recipients[] = $email['recipient'];
             }
             $email = $emails[0];
             $recipients = implode(',', $recipients);
             $message = $email['headers'] . "\r\n\r\n" . $email['body'];
             $structure = Mime_Helper::decode($message, false, false);
             $headers = $structure->headers;
             $headers['to'] = $recipients;
             $headers = Mime_Helper::encodeHeaders($headers);
             $res = Mail_Helper::prepareHeaders($headers);
             if (Misc::isError($res)) {
                 Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
                 return;
             }
             list(, $text_headers) = $res;
             $result = self::_sendEmail($recipients, $text_headers, $email['body'], $status);
             if (Misc::isError($e = $result)) {
                 /** @var PEAR_Error $e */
                 $maq_id = implode(',', $maq_ids);
                 $details = $e->getMessage();
                 $debugInfo = $e->getDebugInfo();
                 if ($debugInfo) {
                     $details .= '/' . $debugInfo;
                 }
                 echo "Mail_Queue: issue #{$email['maq_iss_id']}: Can't send merged mail {$maq_id}: {$details}\n";
                 foreach ($emails as $email) {
                     self::_saveStatusLog($email['id'], 'error', $details);
                 }
                 continue;
             }
             foreach ($emails as $email) {
                 self::_saveStatusLog($email['id'], 'sent', '');
                 if ($email['save_copy']) {
                     Mail_Helper::saveOutgoingEmailCopy($email);
                 }
             }
         }
     }
     foreach (self::_getList($status, $limit) as $maq_id) {
         $email = self::_getEntry($maq_id);
         $result = self::_sendEmail($email['recipient'], $email['headers'], $email['body'], $status);
         if (Misc::isError($e = $result)) {
             /** @var PEAR_Error $e */
             $details = $e->getMessage();
             $debugInfo = $e->getDebugInfo();
             if ($debugInfo) {
                 $details .= '/' . $debugInfo;
             }
             echo "Mail_Queue: issue #{$email['maq_iss_id']}: Can't send mail {$maq_id}: {$details}\n";
             self::_saveStatusLog($email['id'], 'error', $details);
             continue;
         }
         self::_saveStatusLog($email['id'], 'sent', '');
         if ($email['save_copy']) {
             Mail_Helper::saveOutgoingEmailCopy($email);
         }
     }
 }
 /**
  * Sends the queued up messages to their destinations. This can either try
  * to send emails that could not be sent before (status = 'error'), or just
  * emails just recently queued (status = 'pending').
  *
  * @param   string $status The status of the messages that need to be sent
  * @param   integer $limit The limit of emails that we should send at one time
  * @param   boolean $merge Whether or not to send one merged email for multiple entries with the same status and type.
  */
 public static function send($status, $limit = null, $merge = false)
 {
     if ($merge !== false) {
         // TODO: handle self::MAX_RETRIES, but that should be done per queue item
         foreach (self::_getMergedList($status, $limit) as $maq_ids) {
             $emails = self::_getEntries($maq_ids);
             $recipients = array();
             foreach ($emails as $email) {
                 $recipients[] = $email['recipient'];
             }
             $email = $emails[0];
             $recipients = implode(',', $recipients);
             $message = $email['headers'] . "\r\n\r\n" . $email['body'];
             $structure = Mime_Helper::decode($message, false, false);
             $headers = $structure->headers;
             $headers['to'] = $recipients;
             $headers = Mime_Helper::encodeHeaders($headers);
             $res = Mail_Helper::prepareHeaders($headers);
             if (Misc::isError($res)) {
                 Logger::app()->error($res->getMessage(), array('debug' => $res->getDebugInfo()));
                 return;
             }
             list(, $text_headers) = $res;
             $result = self::_sendEmail($recipients, $text_headers, $email['body'], $status);
             if (Misc::isError($e = $result)) {
                 /** @var PEAR_Error $e */
                 $maq_id = implode(',', $maq_ids);
                 $details = $e->getMessage();
                 $debugInfo = $e->getDebugInfo();
                 if ($debugInfo) {
                     $details .= '/' . $debugInfo;
                 }
                 echo "Mail_Queue: issue #{$email['maq_iss_id']}: Can't send merged mail {$maq_id}: {$details}\n";
                 foreach ($emails as $email) {
                     self::_saveStatusLog($email['id'], 'error', $details);
                 }
                 continue;
             }
             foreach ($emails as $email) {
                 self::_saveStatusLog($email['id'], 'sent', '');
                 if ($email['save_copy']) {
                     Mail_Helper::saveOutgoingEmailCopy($email);
                 }
             }
         }
     }
     foreach (self::_getList($status, $limit) as $maq_id) {
         $errors = self::getQueueErrorCount($maq_id);
         if ($errors > self::MAX_RETRIES) {
             // TODO: mark as status 'failed'
             continue;
         }
         $email = self::_getEntry($maq_id);
         $result = self::_sendEmail($email['recipient'], $email['headers'], $email['body'], $status);
         if (Misc::isError($e = $result)) {
             /** @var PEAR_Error $e */
             $details = $e->getMessage();
             $debugInfo = $e->getDebugInfo();
             if ($debugInfo) {
                 $details .= '/' . $debugInfo;
             }
             echo "Mail_Queue: issue #{$email['maq_iss_id']}: Can't send mail {$maq_id}: {$details}\n";
             self::_saveStatusLog($email['id'], 'error', $details);
             continue;
         }
         self::_saveStatusLog($email['id'], 'sent', '');
         if ($email['save_copy']) {
             Mail_Helper::saveOutgoingEmailCopy($email);
         }
     }
 }