You are here

function simplenews_send_test in Simplenews 7.2

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

Send test version of newsletter.

Parameters

mixed $node: The newsletter node to be sent.

Related topics

1 call to simplenews_send_test()
simplenews_node_tab_send_form_submit in includes/simplenews.admin.inc
@todo

File

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

Code

function simplenews_send_test($node, $test_addresses) {

  // Prevent session information from being saved while sending.
  if ($original_session = drupal_save_session()) {
    drupal_save_session(FALSE);
  }

  // Force the current user to anonymous to ensure consistent permissions.
  $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
  // Send newsletter to test addresses.
  // Emails are send direct, not using the spool.
  $recipients = array(
    'anonymous' => array(),
    'user' => array(),
  );
  foreach ($test_addresses as $mail) {
    $mail = trim($mail);
    if (!empty($mail)) {
      $subscriber = simplenews_subscriber_load_by_mail($mail);
      if (!$subscriber) {

        // The source expects a subscriber object with mail and language set.
        // @todo: Find a cleaner way to do this.
        $subscriber = new stdClass();
        $subscriber->uid = 0;
        $subscriber->mail = $mail;
        $subscriber->language = $GLOBALS['language']->language;
      }
      if (!empty($account->uid)) {
        $recipients['user'][] = $account->name . ' <' . $mail . '>';
      }
      else {
        $recipients['anonymous'][] = $mail;
      }
      $source = new SimplenewsSourceNode($node, $subscriber);
      $source
        ->setKey('test');
      $result = simplenews_send_source($source);
    }
  }
  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,
    )));
  }
  $GLOBALS['user'] = $original_user;
  if ($original_session) {
    drupal_save_session(TRUE);
  }
}