/** sendNewsletter()
  *
  * @param int BatchNumber
  *
  * @return boolean full or partial success
  */
 public function sendNewsletter($iBatchNumber = 0)
 {
     if ($this->aRecipients === null || $this->sSenderEmail === null || $this->aMailGroups === null) {
         throw new Exception("Error in sendNewsletter: prepare not called");
     }
     // Send newsletter if newsletter is chosen and there are recipients
     $oNewsletter = NewsletterQuery::create()->findPk($this->iNewsletterId);
     if ($oNewsletter === null) {
         throw new LocalizedException("newsletter.mailing.newsletter_missing");
     }
     if ($iBatchNumber === 0) {
         $this->aUnsuccessfulAttempts = array();
     }
     if ($this->aRecipients instanceof PropelObjectCollection) {
         $this->aRecipients = $this->aRecipients->getData();
     }
     $aRecipients = array_slice($this->aRecipients, $iBatchNumber * $this->iBatchSize, $this->iBatchSize);
     $bRequiresUnsubsribeLink = true;
     $oNewsletterMailer = new NewsletterMailer($oNewsletter, $aRecipients, $bRequiresUnsubsribeLink, $this->sSenderEmail, $this->sSenderName);
     if (!$oNewsletterMailer->send()) {
         $this->aUnsuccessfulAttempts = array_merge($this->aUnsuccessfulAttempts, $oNewsletterMailer->getInvalidEmails());
     }
     if (count($aRecipients) === $this->iBatchSize) {
         return $iBatchNumber + 1;
     } else {
         // Return batch count/boolean and register Mailings per group
         $bOneGroupOnly = count($this->aMailGroups) === 1;
         foreach ($this->aMailGroups as $mMailGroupId) {
             $oNewsletterMailing = new NewsletterMailing();
             $oNewsletterMailing->setDateSent(date('c'));
             if (is_numeric($mMailGroupId)) {
                 $oNewsletterMailing->setSubscriberGroupId($mMailGroupId);
             } else {
                 $oNewsletterMailing->setExternalMailGroupId($mMailGroupId);
             }
             // @todo check change jm > write recipient count if newsletter is sent to singe group only
             if ($bOneGroupOnly) {
                 $oNewsletterMailing->setRecipientCount(count($this->aRecipients));
             }
             $oNewsletterMailing->setNewsletterId($oNewsletter->getId());
             $oNewsletterMailing->save();
         }
         $this->aRecipients = null;
         $this->sSenderEmail = null;
         $this->aMailGroups = null;
         return count($this->aUnsuccessfulAttempts) === 0;
     }
 }