/**
  * @param SproutEmail_EntryModel    $entry
  * @param SproutEmail_CampaignModel $campaign
  *
  * @throws \Exception
  *
  * @return bool
  */
 public function exportEntry(SproutEmail_EntryModel $entry, SproutEmail_CampaignModel $campaign)
 {
     if ($campaign->isNotification()) {
         try {
             return $this->sendMockNotification($entry, $campaign);
         } catch (\Exception $e) {
             throw $e;
         }
     }
     $params = array('entry' => $entry, 'campaign' => $campaign);
     $email = array('fromEmail' => $entry->fromEmail, 'fromName' => $entry->fromName, 'subject' => $entry->subjectLine);
     if ($entry->replyTo && filter_var($entry->replyTo, FILTER_VALIDATE_EMAIL)) {
         $email['replyTo'] = $entry->replyTo;
     }
     $recipients = array();
     if ($entryRecipientLists = SproutEmail_EntryRecipientListRecord::model()->findAllByAttributes(array('entryId' => $entry->id))) {
         foreach ($entryRecipientLists as $entryRecipientList) {
             if ($recipientList = $this->getRecipientListById($entryRecipientList->list)) {
                 $recipients = array_merge($recipients, $recipientList->recipients);
             }
         }
     }
     $email = EmailModel::populateModel($email);
     foreach ($recipients as $recipient) {
         try {
             $params['recipient'] = $recipient;
             $email->body = sproutEmail()->renderSiteTemplateIfExists($campaign->template . '.txt', $params);
             $email->htmlBody = sproutEmail()->renderSiteTemplateIfExists($campaign->template, $params);
             $email->setAttribute('toEmail', $recipient->email);
             $email->setAttribute('toFirstName', $recipient->firstName);
             $email->setAttribute('toLastName', $recipient->lastName);
             craft()->email->sendEmail($email);
         } catch (\Exception $e) {
             throw $e;
         }
     }
 }
 public function deleteRecipientListsByEntryId($id)
 {
     if ($lists = SproutEmail_EntryRecipientListRecord::model()->findAllByAttributes(array('entryId' => $id))) {
         foreach ($lists as $list) {
             $list->delete();
         }
     }
 }
 /**
  * @param SproutEmail_CampaignModel $campaign
  * @param SproutEmail_EntryModel    $entry
  *
  * @throws Exception
  * @throws \Exception
  * @return bool
  */
 public function saveRecipientLists(SproutEmail_CampaignModel $campaign, SproutEmail_EntryModel $entry)
 {
     $mailer = $this->getMailerByName($campaign->mailer);
     if (!$mailer) {
         throw new Exception(Craft::t('The {m} mailer is not supported.', array('m' => $campaign->mailer)));
     }
     sproutEmail()->entries->deleteRecipientListsByEntryId($entry->id);
     $lists = $mailer->prepareRecipientLists($entry, $campaign);
     if ($lists && is_array($lists) && count($lists)) {
         foreach ($lists as $list) {
             $record = SproutEmail_EntryRecipientListRecord::model()->findByAttributes(array('entryId' => $entry->id, 'list' => $list->list));
             $record = $record ? $record : new SproutEmail_EntryRecipientListRecord();
             $record->entryId = $list->entryId;
             $record->mailer = $list->mailer;
             $record->list = $list->list;
             $record->type = $list->type;
             try {
                 $record->save();
             } catch (\Exception $e) {
                 throw $e;
             }
         }
     }
     return true;
 }