You are here

function simplenews_mail in Simplenews 6

Same name and namespace in other branches
  1. 8.2 simplenews.module \simplenews_mail()
  2. 8 simplenews.module \simplenews_mail()
  3. 6.2 simplenews.module \simplenews_mail()
  4. 7.2 simplenews.module \simplenews_mail()
  5. 7 simplenews.module \simplenews_mail()
  6. 3.x simplenews.module \simplenews_mail()

Implementation of hook_mail().

Send simplenews mails using drupal mail API

Parameters

$key: node | test | subscribe | unsubscribe:

array $message message array: [from] [headers][From] [language] : preferred message language

array $params parameter array: [context][node] : node object of message to be sent [context][snid] : used for $key = subscribe or unsubscribe [context][from_name] : name of mail sender or site name (optional) [context][account] : account details of recipient [from] : array('address' => 'noreply@example.org', 'formatted' => 'site name <noreply@example.org>') [newsletter] : newsletter object (tid, name) [tokens] : tokens for variable replacement. Defaults to: user_mail_tokens()

See also

drupal_mail()

File

./simplenews.module, line 1499
Simplnews node handling, sent email, newsletter block and general hooks

Code

function simplenews_mail($key, &$message, $params) {
  $context = $params['context'];
  switch ($key) {
    case 'node':
    case 'test':

      // Message header, body and mail headers are buffered to increase
      // perfomance when sending multiple mails. Buffered data only contains
      // general data, no recipient specific content. Tokens are used
      // for recipient data and will later be replaced.
      // When mailing multiple newsletters in one page call or cron run,
      // data is once stored and subsequently retreived from the
      // static $messages variable.
      // $message buffer is node and language specific.
      static $messages = array();

      // By default the the node is send which is supplied in the function call.
      // When translation is used, the availability of translations is checked
      // and when available the translation of the preferred language is selected.
      $nid = $context['node']->nid;
      $langcode = $message['language']->language;
      if (module_exists('translation')) {

        // If the node has translations and a translation is required
        // the equivalent of the node in the required langugage is used
        // or the base node (nid == tnid) is used.
        if ($tnid = $context['node']->tnid) {
          if ($langcode != $context['node']->language) {
            $translations = translation_node_get_translations($tnid);

            // A translation is available in the preferred language.
            if ($translation = $translations[$langcode]) {
              $nid = $translation->nid;
              $langcode = $translation->language;
            }
            else {

              // No translation found which matches the preferred language.
              foreach ($translations as $translation) {
                if ($translation->nid == $tnid) {
                  $nid = $tnid;
                  $langcode = $translation->language;
                  break;
                }
              }
            }
          }
        }

        // If a translation of the node is used and this node is not available in
        // the message buffer, then load this node.
        if ($nid != $context['node']->nid && !isset($messages[$nid][$langcode])) {
          $context['node'] = node_load($nid);
        }
      }

      // Check if this node-language pair has been buffered.
      // If not, build the message and store it for later use.
      if (!isset($messages[$nid][$langcode])) {

        // Use the default theme to render the email content.
        // We temporary clear the $custom_theme to prevent the admin theme
        // from being used when the newsletter is sent from the
        // node add/edit form and the admin theme is other than the
        // default theme. When no $custom_theme is set, the
        // After theming the email $custom_theme is restored.
        global $custom_theme;
        $org_cutom_theme = $custom_theme;
        $custom_theme = '';
        $node = drupal_clone($context['node']);

        // Add simplenews specific header data
        $headers = array_merge($message['headers'], _simplenews_headers($node, $params['from']['address']));
        $headers['From'] = $params['from']['formatted'];
        $message['headers'] = $messages[$nid][$langcode]['headers'] = $headers;

        // Build email subject
        if ($tid = $node->simplenews['tid']) {
          $term = taxonomy_get_term($tid);

          // Translate the newsletter term name if simplenews vocabulary uses Localized terms.
          if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_LOCALIZE) {
            $name = tt('taxonomy:term:' . $tid . ':name', $term->name, $langcode);
          }
          else {
            $name = $term->name;
          }
        }
        else {
          $name = t('Unassigned newsletter');
        }
        $subject = theme('simplenews_newsletter_subject', $name, $node->title, $message['language']);
        $subject = str_replace(array(
          "\r",
          "\n",
        ), '', $subject);
        $message['subject'] = $messages[$nid][$langcode]['subject'] = $subject;

        // Build message body
        // Processing node body mimics node_view() with full node view
        $node->build_mode = 'email_' . $node->simplenews['s_format'];
        $node = node_build_content($node, FALSE, TRUE);
        $content = drupal_render($node->content);
        $node->body = $content;
        unset($node->teaser);

        // Set a flag to prevent token replacement during node alter.
        $node->simplenews_mail = TRUE;
        node_invoke_nodeapi($node, 'alter', FALSE, TRUE);
        unset($node->simplenews_mail);
        $body = theme(array(
          'simplenews_newsletter_body__' . $context['node']->simplenews['tid'],
          'simplenews_newsletter_body',
        ), $node, $message['language']);

        // Buffer body text node and language specific
        $messages[$nid][$langcode]['body'] = $body;

        // Build and buffer message footer
        $footer = theme(array(
          'simplenews_newsletter_footer__' . $context['node']->simplenews['tid'],
          'simplenews_newsletter_footer',
        ), $context['node'], $key, $message['language']);
        $messages[$nid][$langcode]['footer'] = $footer;

        // Restore the custom theme.
        $custom_theme = $org_cutom_theme;
      }
      else {

        // Get message data from buffer
        $message['headers'] = $messages[$nid][$langcode]['headers'];
        $message['subject'] = $messages[$nid][$langcode]['subject'];
        $body = $messages[$nid][$langcode]['body'];
        $footer = $messages[$nid][$langcode]['footer'];
      }

      // Build message body.
      // Replace tokens with user specific data and
      // Convert to plain text if required.
      $variables = simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default());
      $body = strtr($body, $variables);
      if ($context['node']->simplenews['s_format'] == 'plain') {
        $body = simplenews_html_to_text($body, variable_get('simplenews_hyperlinks_' . $context['node']->simplenews['tid'], 1));
      }
      $message['body']['body'] = $body;

      // Build message footer.
      // Replace tokens with user specific data
      $message['body']['footer'] = strtr($footer, $variables);

      // Add user specific header data.
      $message['headers']['List-Unsubscribe'] = strtr('<!confirm_unsubscribe_url>', $variables);
      break;
    case 'subscribe':

      // Use formatted from address "name" <mail_address>
      $message['headers']['From'] = $params['from']['formatted'];
      $variables = simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default());
      $message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $context['account']->language, $variables);
      if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) {
        $message['body'] = _simplenews_subscription_confirmation_text('subscribe_subscribed', $context['account']->language, $variables);
      }
      else {
        $message['body'] = _simplenews_subscription_confirmation_text('subscribe_unsubscribed', $context['account']->language, $variables);
      }
      break;
    case 'unsubscribe':

      // Use formatted from address "name" <mail_address>
      $message['headers']['From'] = $params['from']['formatted'];
      $variables = simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default());
      $message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $context['account']->language, $variables);
      if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) {
        $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_subscribed', $context['account']->language, $variables);
      }
      else {
        $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_unsubscribed', $context['account']->language, $variables);
      }
      break;
  }

  // Debug message to check for outgoing emails messages.
  // Debug message of node and test emails is set in simplenews_mail_mail().
  if (variable_get('simplenews_debug', FALSE) && $key != 'node' && $key != 'test') {
    watchdog('simplenews', 'Outgoing email. Message type: %type<br />Subject: %subject<br />Recipient: %to', array(
      '%type' => $key,
      '%to' => $message['to'],
      '%subject' => $message['subject'],
    ), WATCHDOG_DEBUG);
  }
}