You are here

function simplenews_send_test in Simplenews 6.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_send_test()
  2. 6 simplenews.module \simplenews_send_test()
  3. 7.2 includes/simplenews.mail.inc \simplenews_send_test()
  4. 7 includes/simplenews.mail.inc \simplenews_send_test()

Send test version of newsletter.

Parameters

integer or object $node Newsletter node to be sent. Integer = nid; Object = node object:

1 call to simplenews_send_test()
simplenews_node_tab_send_form_submit in ./simplenews.module
Simplenews tab form submit callback

File

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

Code

function simplenews_send_test($node) {
  if (is_numeric($node)) {
    $node = node_load($node);
  }
  if (is_object($node)) {

    // switch to anonymous user - needed in foreground spool sends, adopted from D7 drupal_cron_run().
    // see #471594 for issue about sending with higher permissions
    // note that private (non-public) fields might be invisible in newsletters now always.
    $original_user = $GLOBALS['user'];
    $GLOBALS['user'] = drupal_anonymous_user();

    // Send the test newsletter to the test address(es) specified in the node.
    // Build array of test email addresses
    $mails = explode(',', $node->simplenews['test_address']);

    // Send newsletter to test addresses.
    // Emails are send direct, not using the spool.
    $recipients = array(
      'anonymous' => array(),
      'user' => array(),
    );
    foreach ($mails as $mail) {
      $mail = trim($mail);
      if (!empty($mail)) {
        $account = _simplenews_user_load($mail);
        if ($account->uid) {
          $recipients['user'][] = $account->name . ' <' . $mail . '>';
        }
        else {
          $recipients['anonymous'][] = $mail;
        }
        $tmpres = simplenews_mail_mail($node->nid, $node->vid, $mail, 'test');
      }
    }
    if (count($recipients['user'])) {
      $recipients_txt = implode(', ', $recipients['user']);
      drupal_set_message(t('Test newsletter sent to user %recipient.', array(
        '%recipient' => $recipients_txt,
      )));
    }
    if (count($recipients['anonymous'])) {
      $recipients_txt = implode(', ', $recipients['anonymous']);
      drupal_set_message(t('Test newsletter sent to anonymous %recipient.', array(
        '%recipient' => $recipients_txt,
      )));
    }

    // Restore the user.
    $GLOBALS['user'] = $original_user;
  }
}