/**
  * Sends the newsletters.
  */
 protected function sendNewsletters()
 {
     $templateName = 'newsletterMail';
     //Sends mail to all subscribers.
     foreach ($this->outstandingNewsletters as $id => $newsletter) {
         $text = $newsletter['text'];
         //workaround to make sure that the template is found
         $templatePaths = array(WCF_DIR . 'templates/', WCF_DIR . 'acp/templates/');
         WCF::getTPL()->setTemplatePaths($templatePaths);
         $newsletterObj = new ViewableNewsletter($id);
         $emailText = $newsletterObj->getFormattedMessage();
         WCF::getTPL()->assign(array('subject' => $newsletter['subject'], 'text' => $emailText));
         $content = WCF::getTPL()->fetch($templateName);
         $i = 0;
         usleep(1);
         //sending one mail per subscriber
         //is longer, but safer
         foreach ($this->subscribersList as $subscriber) {
             //sleep 2 seconds after 10 sent mails
             if (fmod($i, 10) == 0) {
                 usleep(2000000);
             }
             $unsubscribeToken = '';
             if (!isset($this->unsubscribeTokens[$subscriber['subscriberID']])) {
                 $unsubscribeToken = StringUtil::getRandomID();
                 $sql = 'INSERT INTO wcf' . WCF_N . '_' . $this->unsubscriptionTable . '
             			(subscriberID, token)
             		VALUES
             			(' . intval($subscriber['subscriberID']) . ", '" . escapeString($unsubscribeToken) . "')";
                 WCF::getDB()->sendQuery($sql);
             } else {
                 $unsubscribeToken = $this->unsubscribeTokens[$subscriber['subscriberID']]['token'];
             }
             $recipient = null;
             if ($subscriber['userID']) {
                 $recipient = new User($subscriber['userID']);
                 // check for non receiving groups
                 if (!NewsletterUtil::canReceiveNewsletters($recipient)) {
                     continue;
                 }
             }
             // {$username} stands for the username of the specific subscriber
             if (is_null($recipient) || $recipient->getUserOption('acceptNewsletterAsEmail')) {
                 $tmpContent = str_replace('{$username}', $subscriber['username'], $content);
                 $tmpContent = str_replace('subscriberID', $subscriber['subscriberID'], $tmpContent);
                 $tmpContent = str_replace('token', $unsubscribeToken, $tmpContent);
                 $email = $subscriber['email'];
                 $mail = new Mail($email, $newsletter['subject'], $tmpContent, MESSAGE_NEWSLETTERSYSTEM_GENERAL_FROM);
                 //$mail->addBCC(MAIL_ADMIN_ADDRESS); would result in x mails
                 $mail->setContentType('text/html');
                 $mail->send();
             }
             if (!is_null($recipient) && $recipient->getUserOption('acceptNewsletterAsPM')) {
                 $recipientArray = array();
                 $recipientArray[] = array('userID' => $subscriber['userID'], 'username' => $subscriber['username']);
                 $admin = new User(MESSAGE_NEWSLETTERSYSTEM_GENERAL_ADMIN);
                 $options = array('enableSmilies' => $newsletter['enableSmilies'], 'enableHtml' => $newsletter['enableHtml'], 'enableBBCodes' => $newsletter['enableBBCodes']);
                 $tmpText = str_replace('{$username}', $subscriber['username'], $text);
                 $pm = PMEditor::create(false, $recipientArray, array(), $newsletter['subject'], $tmpText, $admin->userID, $admin->username, $options);
             }
             $i++;
         }
     }
     WCF::getCache()->clearResource('newsletter-subscriber-' . PACKAGE_ID);
 }
 /**
  * Sends a testmail of the given newsletter.
  *
  * @param Newsletter $newsletter
  */
 protected function sendTestmail(Newsletter $newsletter)
 {
     $newsletterID = $newsletter->newsletterID;
     //workaround to make sure that the template is found
     $templatePaths = array(WCF_DIR . 'templates/', WCF_DIR . 'acp/templates/');
     WCF::getTPL()->setTemplatePaths($templatePaths);
     $newsletterObj = new ViewableNewsletter($newsletterID);
     $emailText = $newsletterObj->getFormattedMessage();
     WCF::getTPL()->assign(array('subject' => $newsletter->subject, 'text' => $emailText));
     $templateName = 'newsletterMail';
     $content = WCF::getTPL()->fetch($templateName);
     $admin = new User(MESSAGE_NEWSLETTERSYSTEM_GENERAL_ADMIN);
     $tmpContent = str_replace('{$username}', $admin->username, $content);
     $tmpContent = str_replace('subscriberID', 1, $tmpContent);
     $tmpContent = str_replace('token', 'test', $tmpContent);
     $email = $admin->email;
     $mail = new Mail($email, $newsletter->subject, $tmpContent, MESSAGE_NEWSLETTERSYSTEM_GENERAL_FROM);
     $mail->setContentType('text/html');
     $mail->send();
     //resetting cache
     $cacheName = 'newsletter-' . PACKAGE_ID;
     WCF::getCache()->clear(WCF_DIR . 'cache/', 'cache.' . $cacheName . '.php');
     HeaderUtil::redirect('index.php?form=NewsletterEdit&newsletterID=' . $newsletterID . '&packageID=' . PACKAGE_ID . SID_ARG_2ND_NOT_ENCODED);
     exit;
 }