You are here

function simplenews_mail_mail in Simplenews 6.2

Same name and namespace in other branches
  1. 6 simplenews.module \simplenews_mail_mail()

Send a node to an email address.

Parameters

$nid node id of newsletter node:

$vid revision id of newsletter node:

$mail target email address:

$key email key [node|test]:

$reset: Reset static cache.

Return value

TRUE if email is successfully delivered by php mail()

3 calls to simplenews_mail_mail()
SimplenewsSendTestCase::setUp in tests/simplenews.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
simplenews_mail_spool in includes/simplenews.mail.inc
Send simplenews newsletters from the spool.
simplenews_send_test in includes/simplenews.mail.inc
Send test version of newsletter.

File

includes/simplenews.mail.inc, line 167
Simplenews email send and spool handling

Code

function simplenews_mail_mail($nid, $vid, $mail, $key = 'node', $reset = FALSE) {
  static $cache = array();
  if ($reset) {
    $cache = array();
    return;
  }

  // Get subscription data for recipient and language
  $account = new stdClass();
  $account->mail = $mail;
  $subscription = simplenews_get_subscription($account);
  $params['context']['account'] = $subscription;

  // Get node data for the mail
  // Because node_load() only caches the most recent node we cache here based on nid and vid.
  if (isset($cache["{$nid}:{$vid}"])) {
    $node = $cache["{$nid}:{$vid}"];
  }
  else {
    $node = node_load($nid, $vid);
    $cache["{$nid}:{$vid}"] = $node;
  }
  if (is_object($node)) {
    $params['from'] = _simplenews_set_from($node);
    $params['context']['newsletter'] = taxonomy_get_term($node->simplenews['tid']);
    $params['context']['node'] = $node;

    // Send mail
    if (module_exists('mimemail')) {

      // If mimemail module is installed ALL emails are send via this module.
      // drupal_mail() builds the content of the email but does NOT send.
      $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['formatted'], FALSE);
      $to = isset($message['params']['context']['account']) ? $message['params']['context']['account'] : $message['to'];
      $plain = $message['params']['context']['node']->simplenews['s_format'] == 'plain' ? TRUE : NULL;
      $mimemail_result = mimemail($message['from'], $to, $message['subject'], $message['body'], $plain, $message['headers'], $plain ? $message['body'] : simplenews_html_to_text($message['body'], TRUE), isset($message['params']['context']['node']->files) ? $message['params']['context']['node']->files : array(), $message['id']);

      // Mimemail has changed its API (see http://drupal.org/node/808518) but we keep backward compatibility
      if (is_array($mimemail_result)) {
        $message = $mimemail_result;
      }
      else {
        $message['result'] = $mimemail_result;
      }
    }
    else {
      $message = drupal_mail('simplenews', $key, $subscription->mail, $subscription->language, $params, $params['from']['formatted'], TRUE);
    }

    // Log sent result in watchdog.
    if (variable_get('simplenews_debug', FALSE)) {
      $via_mimemail = '';
      if (module_exists('mimemail')) {
        $via_mimemail = t('Sent via Mime Mail');
      }

      //TODO Add line break before %mimemail.
      if ($message['result']) {
        watchdog('simplenews', 'Outgoing email. Message type: %type<br />Subject: %subject<br />Recipient: %to %mimemail', array(
          '%type' => $key,
          '%to' => $message['to'],
          '%subject' => $message['subject'],
          '%mimemail' => $via_mimemail,
        ), WATCHDOG_DEBUG);
      }
      else {
        watchdog('simplenews', 'Outgoing email failed. Message type: %type<br />Subject: %subject<br />Recipient: %to %mimemail', array(
          '%type' => $key,
          '%to' => $message['to'],
          '%subject' => $message['subject'],
          '%mimemail' => $via_mimemail,
        ), WATCHDOG_ERROR);
      }
    }

    // Build array of sent results for spool table and reporting.
    if ($message['result']) {
      $message['result'] = array(
        'status' => SIMPLENEWS_SPOOL_DONE,
        'error' => FALSE,
      );
    }
    else {

      // This error may be caused by faulty mailserver configuration or overload.
      // Some systems try to contact the target server immediately and return error if the domain or mail account is nonexistent. See #780132.
      // For now stop sending as this will result in an infinite loop.
      // @todo: Add counter of tries and abort after N tries. Build stats.
      $message['result'] = array(
        'status' => SIMPLENEWS_SPOOL_HOLD,
        'error' => TRUE,
      );
    }
  }
  else {

    // Node could not be loaded. The node is probably deleted while pending to be sent.
    // This error is not recoverable, mark "done".
    $message['result'] = array(
      'status' => SIMPLENEWS_SPOOL_DONE,
      'error' => TRUE,
    );
    watchdog('simplenews', 'Newsletter not sent: newsletter issue does not exist (nid = @nid; vid = @vid).', array(
      '@nid' => $nid,
      '@vid' => $vid,
    ), WATCHDOG_ERROR);
  }
  return isset($message['result']) ? $message['result'] : FALSE;
}