You are here

function newsletter_mail in Newsletter 7

Same name and namespace in other branches
  1. 7.2 newsletter.module \newsletter_mail()

Implements hook_mail().

File

./newsletter.module, line 692
Defines menu items for newsletter administration, permissions and basic drupal hooks.

Code

function newsletter_mail($key, &$message, $params) {
  $template = isset($params['template']) ? $params['template'] : array();
  $subscriber = isset($params['subscriber']) ? $params['subscriber'] : array();
  $nodes = isset($params['nodes']) ? $params['nodes'] : array();
  $list = isset($params['list']) ? $params['list'] : array();
  $format = isset($params['format']) ? $params['format'] : '';
  $newsletter = isset($params['newsletter']) ? $params['newsletter'] : array();
  $template_body = field_get_items('newsletter_template', $template, 'field_newsletter_body');
  if ($template_body) {
    $template_body = $template_body[0];
  }
  switch ($key) {
    case 'test':
      $message['subject'] = t('Newsletter test e-mail');
      $message['body'][] = t('This is a test e-mail generated by the Drupal Newsletter module.
      If you succesfully received this e-mail,
      it means that your server is capable of sending e-mails.
      Congratulations!
      You can now create lists, templates and subscribe users to them!');
      $message['format'] = $params['format'];
      $message['body_format'] = 'plain_text';
      break;
    case 'basic':
      $body = token_replace($template_body['value'], array(
        'newsletter_subscriber' => $subscriber,
      ));
      $message['subject'] = token_replace($template->subject, array());
      $message['body'][] = $body;
      $message['body_format'] = $template_body['format'];
      $message['format'] = isset($subscriber->receive_format) && $subscriber->receive_format ? $subscriber->receive_format : $format;
      break;
    case 'automated':
      if (strpos($template_body['value'], '[repeat]') && strpos($template_body['value'], '[/repeat]')) {
        $body = explode('[repeat]', $template_body['value']);
        $head = $body[0];
        $body = explode('[/repeat]', $body[1]);
        $foot = $body[1];
        $items = array();
        foreach ($nodes as $node) {
          $token_params = array(
            'node' => $node,
          );

          // Allow other modules to add token params for token_replace().
          foreach (module_implements('newsletter_automated_token_params') as $module) {
            $function = $module . '_newsletter_automated_token_params';
            $function($token_params);
          }
          $items[] = token_replace($body[0], $token_params);
        }
        $body = implode("\n", $items);
        $body = $head . $body . $foot;
      }
      else {
        $body = $template_body['value'];
      }
      $body = token_replace($body, array(
        'newsletter_subscriber' => $subscriber,
        'newsletter_list' => $list,
      ));
      $message['subject'] = token_replace($template->subject, array());
      $message['body'][] = $body;
      $message['format'] = isset($subscriber->receive_format) && $subscriber->receive_format ? $subscriber->receive_format : $format;
      $message['body_format'] = $template_body['format'];
      if (variable_get('newsletter_track_open_rate')) {
        $message['body'][] = newsletter_add_open_rate_image($subscriber->hash, $newsletter);
      }
      break;
    case 'custom':
      $body = token_replace($template_body['value'], array(
        'newsletter_subscriber' => $subscriber,
      ));
      $message['subject'] = token_replace($template->subject, array());
      $message['body'][] = $body;
      $message['format'] = isset($subscriber->receive_format) && $subscriber->receive_format ? $subscriber->receive_format : $format;
      $message['body_format'] = $template_body['format'];
      if (variable_get('newsletter_track_open_rate')) {
        $message['body'][] = newsletter_add_open_rate_image($subscriber->hash, $newsletter);
      }
      break;
  }
}