You are here

public function NewsletterAutomated::send in Newsletter 7

Same name and namespace in other branches
  1. 7.2 includes/newsletter.automated.inc \NewsletterAutomated::send()

Replaces tokens and sends the current newsletter.

File

includes/newsletter.automated.inc, line 243
Contains NewsletterMail and NewsletterCustom that extend NewsletterBasic.

Class

NewsletterAutomated
Newsletter class that sends automated, non-custom newsletters with dynamic content based on taxonomy terms.

Code

public function send() {
  $i = 0;
  $mails_to_send = variable_get('newsletter_cron_number', 500);
  $params = array(
    'template' => $this->template,
    'subscriber' => '',
    'list' => $this->list,
    'format' => $this->format,
    'newsletter' => $this->newsletter,
  );
  if (empty($this->subscribers)) {
    return array(
      'No subscribers',
    );
  }
  foreach ($this->subscribers as $subscriber) {
    $language = isset($subscriber->language) ? newsletter_language_list($subscriber->language) : $this->language;
    $nodes = $this->nodes == 'exposed' ? $this
      ->getSubscriberNodes($subscriber) : $this->nodes;
    if (empty($nodes)) {
      continue;
    }
    $params['nodes'] = $nodes;
    if ($i >= $mails_to_send) {
      $subscriber->nodes = $nodes;
      $this->left[$this->list->nlid][] = $subscriber;
      continue;
    }
    $params['subscriber'] = $subscriber;
    $message = drupal_mail('newsletter', 'automated', $subscriber->email, $language, $params, $this->from);
    if (!$message['result']) {
      newsletter_set_watchdog($this->newsletter->nnid, $this->list->title, $subscriber->email, $result);
    }
    $status[] = $message['result'];
    $i++;
  }
  variable_set('newsletter_for_next_cron', $this->left);
  if (empty($this->left[$this->list->nlid]) && isset($message)) {
    module_invoke_all('newsletter_list_sent', $message);
  }
  $this
    ->updateStats($i);
  return isset($status) ? $status : array(
    'No items',
  );
}