You are here

function simplenews_send_node in Simplenews 6.2

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

Send newsletter node to subscribers.

Parameters

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

array $accounts account objects to send the newsletter to.: account = object ( snid = subscription id, or 0 if no subscription record exists. tids = array(tid) array of newsletter tid's. uid = user id, or 0 if subscriber is anonymous user. mail = user email address. name = <empty>. Added for compatibility with user account object. language = language object. User-preferred or default language. ) NOTE: either snid, mail or uid is required.

2 calls to simplenews_send_node()
simplenews_node_tab_send_form_submit in ./simplenews.module
Simplenews tab form submit callback
simplenews_send_newsletter_action in simplenews_action/simplenews_action.module
A configurable Drupal action. Send a simplenews newsletter. hook = cron: Send a newsletter to all subscribers. hook = user: Send a newsletter to the user who triggered the action.

File

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

Code

function simplenews_send_node($node, $accounts = array()) {
  $mails = array();

  // We always start with an empty spooler
  simplenews_clear_spool_node($node);
  if (is_numeric($node)) {
    $node = node_load($node);
  }
  if (is_object($node)) {
    $from = _simplenews_set_from($node);
    $params['context']['node'] = $node;
    $params['from'] = $from;
    $node_data['tid'] = $node->simplenews['tid'];
    $node_data['nid'] = $node->nid;
    $node_data['vid'] = $node->vid;
    $sent_subscriber_count = 0;
    if (empty($accounts)) {

      // No accounts specified.  Write all active subscriber addresses to mail spool.
      db_query('
        INSERT INTO {simplenews_mail_spool}
        (mail, nid, vid, tid, status, timestamp)
        SELECT s.mail, %d, %d, t.tid, %d, %d
        FROM {simplenews_subscriptions} s
        INNER JOIN {simplenews_snid_tid} t
          ON s.snid = t.snid
        WHERE s.activated = 1
          AND t.tid = %d
          AND t.status = %d', $node->nid, $node->vid, SIMPLENEWS_SPOOL_PENDING, time(), $node->simplenews['tid'], SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED);
      $sent_subscriber_count = db_affected_rows();
    }
    else {

      // Get email address of specified accounts.
      foreach ($accounts as $account) {
        $account = simplenews_get_subscription($account);
        $mails[] = array(
          'mail' => $account->mail,
        );
      }
      $sent_subscriber_count = count($mails);
    }

    // Persist subscriber count now
    db_query("\n      UPDATE {simplenews_newsletters}\n      SET sent_subscriber_count = %d\n      WHERE nid = %d", $sent_subscriber_count, $node->nid);

    // To send the newsletter, the node id and target email addresses
    // are stored in the spool.
    // When cron is not used the newsletter is send immediately to the emails
    // in the spool. When cron is used newsletters are send to addresses in the
    // spool during the next (and following) cron run.
    foreach ($mails as $mail) {
      $data = array_merge($node_data, $mail);
      simplenews_save_spool($data);
    }
    if (variable_get('simplenews_use_cron', TRUE) == FALSE) {
      simplenews_mail_spool($node_data['nid'], $node_data['vid'], 999999);
      simplenews_clear_spool();

      // Update sent status for newsletter admin panel.
      simplenews_send_status_update();
      drupal_set_message(t('Newsletter sent.'));
      simplenews_clear_spool();
    }
    else {
      drupal_set_message(t('Newsletter pending.'));
    }
  }
}