You are here

function simplenews_send_newsletter_action in Simplenews 6.2

Same name and namespace in other branches
  1. 6 simplenews_action/simplenews_action.module \simplenews_send_newsletter_action()

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.

Available context: $context['nid'] newsletter id $context['title'] newsletter title $context['resend'] allow resending of a previously send newsletter

See also

simplenews_send_newsletter_action_form()

simplenews_send_newsletter_action_submit()

File

simplenews_action/simplenews_action.module, line 78
Provide actions for simplenews.

Code

function simplenews_send_newsletter_action(&$object, $context = array()) {
  if ($context['hook'] == 'cron' || $context['hook'] == 'user' || $context['hook'] == 'simplenews') {
    module_load_include('inc', 'simplenews', 'includes/simplenews.mail');

    // Determine newsletter recipients
    $accounts = array();
    if ($context['hook'] == 'user' || $context['hook'] == 'simplenews') {
      $accounts[] = $context['account'];
    }

    // Get sent status of this newsletter. The global newsletter sent status is used not the sent status of individual emails.
    $s_status = db_result(db_query("\n      SELECT s_status\n      FROM {simplenews_newsletters}\n      WHERE nid = %d", $context['nid']));

    // Send newsletter if resend is allowed OR newsletter is not yet send.
    if ($context['resend'] or $s_status == SIMPLENEWS_STATUS_SEND_NOT) {
      if ($context['hook'] == 'cron') {

        // Set newsletter sent status to pending if send by cron.
        db_query("\n          UPDATE {simplenews_newsletters}\n          SET s_status = %d\n          WHERE nid = %d", SIMPLENEWS_STATUS_SEND_PENDING, $context['nid']);
      }
      simplenews_send_node($context['nid'], $accounts);
    }
    watchdog('action', 'Simplenews newsletter %title send.', array(
      '%title' => $context['title'],
    ));
  }
}