Example #1
0
File: Queue.php Project: roojs/pear
 /**
  * Send mails fom queue.
  *
  * Mail_Queue::sendMailsInQueue()
  *
  * @param integer $limit     Optional - max limit mails send.
  *                           This is the max number of emails send by
  *                           this function.
  * @param integer $offset    Optional - you could load mails from $offset (by id)
  * @param integer $try       Optional - hoh many times mailqueu should try send
  *                           each mail. If mail was sent succesful it will be
  *                           deleted from Mail_Queue.
  * @param mixed   $callback  Optional, a callback (string or array) to save the
  *                           SMTP ID and the SMTP greeting.
  *
  * @return mixed  True on success else MAILQUEUE_ERROR object.
  */
 function sendMailsInQueue($limit = MAILQUEUE_ALL, $offset = MAILQUEUE_START, $try = MAILQUEUE_TRY, $callback = null)
 {
     if (!is_int($limit) || !is_int($offset) || !is_int($try)) {
         return Mail_Queue::raiseError("sendMailsInQueue(): limit, offset and try must be integer.", MAILQUEUE_ERROR_UNEXPECTED);
     }
     if ($callback !== null) {
         if (!is_array($callback) && !is_string($callback)) {
             return Mail_Queue::raiseError("sendMailsInQueue(): callback must be a string or an array.", MAILQUEUE_ERROR_UNEXPECTED);
         }
     }
     $this->container->setOption($limit, $offset, $try);
     while (($mail = $this->get()) && !PEAR::isError($mail)) {
         $this->container->countSend($mail);
         $result = $this->sendMail($mail, true);
         if (PEAR::isError($result)) {
             //remove the problematic mail from the buffer, but don't delete
             //it from the db: it might be a temporary issue.
             $this->container->skip();
             PEAR::raiseError('Error in sending mail: ' . $result->getMessage(), MAILQUEUE_ERROR_CANNOT_SEND_MAIL, PEAR_ERROR_TRIGGER, E_USER_NOTICE);
             continue;
         }
         //take care of callback first, as it may need to retrieve extra data
         //from the mail_queue table.
         if ($callback !== null) {
             $queued_as = null;
             $greeting = null;
             if (isset($this->queued_as)) {
                 $queued_as = $this->queued_as;
             }
             if (isset($this->greeting)) {
                 $greeting = $this->greeting;
             }
             call_user_func($callback, array('id' => $mail->getId(), 'queued_as' => $queued_as, 'greeting' => $greeting));
         }
         // delete email from queue?
         if ($mail->isDeleteAfterSend()) {
             $status = $this->deleteMail($mail->getId());
         }
         unset($mail);
         if (isset($this->mail_options['delay']) && $this->mail_options['delay'] > 0) {
             sleep($this->mail_options['delay']);
         }
     }
     // most likely from breaking the loop
     if (isset($mail) && PEAR::isError($mail)) {
         return $mail;
     }
     if (!empty($this->mail_options['persist']) && is_object($this->send_mail)) {
         $this->send_mail->disconnect();
     }
     return true;
 }