You are here

function simplenews_mail_mail in Simplenews 6

Same name and namespace in other branches
  1. 6.2 includes/simplenews.mail.inc \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]:

Return value

TRUE if email is succesfully delivered by php mail()

2 calls to simplenews_mail_mail()
simplenews_mail_spool in ./simplenews.module
Send simplenews newsletters from the spool.
simplenews_send_test in ./simplenews.module
Send test version of newsletter.

File

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

Code

function simplenews_mail_mail($nid, $vid, $mail, $key = 'node') {

  // 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
  $node = node_load($nid, $vid);
  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.
      // Mark "pending" to keep trying.
      $message['result'] = array(
        'status' => SIMPLENEWS_SPOOL_PENDING,
        '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 send: newsletter issue does not exist (nid = @nid; vid = @vid).', array(
      '@nid' => $message['nid'],
      '@vid' => $message['vid'],
    ), WATCHDOG_ERROR);
  }
  return isset($message['result']) ? $message['result'] : FALSE;
}