Example #1
0
 /**
  * Send a bulk message. It overrides the abstract method defined in Message
  * class.
  *
  * Hint: it starts to send messages from the the $start-th subscribers of
  * the message
  *
  * @access	public
  * @param	int $start_time subscription time to begin to
  * @param	int $start_id subscriber identifier to begin to
  * @return  array containing next suscription time, subscriber id and number of sent messages
  * @since	0.6
  */
 function send($start_time = 0, $start_id = 0, $continue_on_error = false)
 {
     jincimport('utility.servicelocator');
     $servicelocator = ServiceLocator::getInstance();
     $logger = $servicelocator->getLogger();
     jimport('joomla.filesystem.folder');
     jimport('joomla.filesystem.file');
     jincimport('utility.parameterprovider');
     $mailmsg = JFactory::getMailer();
     $root_uri = JURI::root();
     $sleeptime = ParameterProvider::getMailTimeInterval();
     $max_mails = ParameterProvider::getMaxXStep();
     $max_bulk_bcc = ParameterProvider::getMailMaxBcc();
     $send_mail = ParameterProvider::getSendMail();
     $logger->finer('BulkMessage: Sleep time ' . $sleeptime . ' - Max mails ' . $max_mails . ' Max BCC ' . $max_bulk_bcc . ' Send mail ' . $send_mail);
     $last_time = $start_time;
     $last_id = $start_id;
     $nmessages = 0;
     $nsuccess = 0;
     if ($newsletter = $this->loadNewsletter()) {
         ob_start();
         $logger->finer('BulkMessage: Newsletter loaded');
         $msg = $this->get('body') . $newsletter->get('disclaimer');
         // Newsletter info substitution in message body
         $msg = preg_replace('/\\[SENDER\\]/s', $newsletter->get('sendername'), $msg);
         $msg = preg_replace('/\\[SENDERMAIL\\]/s', $newsletter->get('senderaddr'), $msg);
         $msg = preg_replace('/\\[NEWSLETTER\\]/s', $newsletter->get('name'), $msg);
         $news_id = $newsletter->get('id');
         $unsub_link = JURI::root() . 'index.php?option=com_jinc&view=newsletter&layout=unsubscription&id=' . $news_id;
         $msg = preg_replace('/\\[UNSUBSCRIPTIONURL\\]/s', $unsub_link, $msg);
         $msg = preg_replace('#src[ ]*=[ ]*\\"(?!https?://)(?:\\.\\./|\\./|/)?#', 'src="' . $root_uri, $msg);
         $msg = preg_replace('#href[ ]*=[ ]*\\"(?!https?://)(?!mailto?:)(?!tel?:)(?:\\.\\./|\\./|/)?#', 'href="' . $root_uri, $msg);
         $msg = preg_replace('#url[ ]*\\(\'(?!https?://)(?:\\.\\./|\\./|/)?#', 'url(\'' . $root_uri, $msg);
         $subject = $this->get('subject');
         $subject = preg_replace('/\\[SENDER\\]/s', $newsletter->get('sendername'), $subject);
         $subject = preg_replace('/\\[SENDERMAIL\\]/s', $newsletter->get('senderaddr'), $subject);
         $subject = preg_replace('/\\[NEWSLETTER\\]/s', $newsletter->get('name'), $subject);
         // Setting message general properties
         $mailmsg->ContentType = $this->get('plaintext') ? "text/plain" : "text/html";
         $mailmsg->setSubject($subject);
         if (strlen($newsletter->get('senderaddr')) > 0) {
             $mailmsg->setSender(array($newsletter->get('senderaddr'), $newsletter->get('sendername')));
         }
         if (strlen($newsletter->get('replyto_addr')) > 0) {
             $mailmsg->addReplyTo(array($newsletter->get('replyto_addr'), $newsletter->get('replyto_name')));
         }
         $path_abs_root = JPATH_ROOT;
         $msg_attachment = $this->get('attachment');
         $arr_attachment = $msg_attachment->toArray();
         foreach ($arr_attachment as $key => $value) {
             $attachment = str_replace('/', DIRECTORY_SEPARATOR, $value);
             if (strlen($attachment)) {
                 $logger->finer('BulkMessage: adding attachment ' . $path_abs_root . DIRECTORY_SEPARATOR . $attachment);
                 $mailmsg->addAttachment($path_abs_root . DIRECTORY_SEPARATOR . $attachment);
             }
         }
         $logger->finer('BulkMessage: going to add recipients');
         $recipients = $newsletter->getSubscribersList($start_time, $start_id, $max_bulk_bcc);
         $nrecips = count($recipients);
         $logger->finer('BulkMessage: found ' . $nrecips . ' subscribers');
         $success = true;
         ob_end_clean();
         for ($i = 0; $i < $nrecips && $success; $i += $max_bulk_bcc) {
             ob_start();
             $ndest = 0;
             for ($j = 0; $j < $max_bulk_bcc && $j + $i < $nrecips; $j++) {
                 $current = $recipients[$j + $i];
                 $this->reported_recipients[$current['id']] = 0;
                 if (!$mailmsg->addBCC($current['email'])) {
                     $this->reported_recipients[$current['id']] = 1;
                 }
                 $ndest = $ndest + 1;
                 $last_time = $current['last_time'];
                 $last_id = $current['last_id'];
             }
             $mailmsg->setBody($this->emogrify($msg));
             $nmessages = $nmessages + $ndest;
             if ($send_mail) {
                 $success = $mailmsg->Send();
                 if ($success === true) {
                     $nsuccess = $nsuccess + $ndest * $success;
                     $logger->finer('BulkMessage: success sending mail.');
                 } else {
                     $logger->warning('BulkMessage: error sending mail.');
                     $this->mail_system_error = ob_get_contents();
                     $logger->warning('BulkMessage: error sending mail. MSG = ' . $this->mail_system_error);
                     for ($j = 0; $j < $max_bulk_bcc && $j + $i < $nrecips; $j++) {
                         $current = $recipients[$j + $i];
                         $this->reported_recipients[$current['id']] = 2;
                     }
                     if (!$continue_on_error) {
                         $success = false;
                     }
                 }
             } else {
                 $bcc_addresses = array();
                 foreach ($mailmsg->bcc as $number_variable => $variable) {
                     array_push($bcc_addresses, $mailmsg->bcc[$number_variable][0]);
                 }
                 $logger->info('BulkMessage: simulate sending mail. BCC = ' . implode(', ', $bcc_addresses));
                 $logger->info('BulkMessage: simulate sending mail. Body = ' . $msg);
                 $nsuccess = $nsuccess + $ndest;
             }
             // socket_select($read = NULL, $write = NULL, $sock = array(socket_create (AF_INET, SOCK_RAW, 0)), 0, $sleeptime);
             usleep($sleeptime);
             $mailmsg->ClearAllRecipients();
             ob_end_clean();
         }
     } else {
         $this->setError('COM_JINC_ERR001');
         return false;
     }
     return array('last_time' => $last_time, 'last_id' => $last_id, 'nmessages' => $nmessages, 'nsuccess' => $nsuccess);
 }