You are here

function simplenews_unsubscribe_user in Simplenews 7

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_unsubscribe_user()
  2. 6.2 simplenews.module \simplenews_unsubscribe_user()
  3. 6 simplenews.module \simplenews_unsubscribe_user()

Unsubscribe a user from a mailing list or send a confirmation mail.

The $confirm parameter determines the action: FALSE = The user is unsubscribed TRUE = User receives an email to verify the address and complete the subscription cancellation

Parameters

$mail: The email address to unsubscribe from the mailing list.

$tid: The term ID of the list.

$confirm: If TRUE, send a confirmation mail; if FALSE, unsubscribe immediately.

$source: Indicates the unsubscribe source. Simplenews uses these sources:

  • website: Via any website form (with or without confirmation email).
  • mass subscribe: Mass admin UI.
  • mass unsubscribe: Mass admin UI.
  • action: Drupal actions.

Related topics

11 calls to simplenews_unsubscribe_user()
SimpleNewsAdministrationTestCase::testSubscriptionManagement in tests/simplenews.test
Test newsletter subscription management.
SimplenewsSourceTestCase::testSendCaching in tests/simplenews.test
Test sending a newsletter to 100 recipients with caching enabled.
simplenews_block_form_submit in includes/simplenews.subscription.inc
simplenews_confirm_multi_form_submit in includes/simplenews.subscription.inc
simplenews_confirm_removal_form_submit in includes/simplenews.subscription.inc

... See full list

File

./simplenews.module, line 1378
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_unsubscribe_user($mail, $tid, $confirm = TRUE, $source = 'unknown') {
  $subscriber = simplenews_subscriber_load_by_mail($mail);

  // The unlikely case that a user is unsubscribed from a non existing mailing list is logged
  if (!($category = simplenews_category_load($tid))) {
    watchdog('simplenews', 'Attempt to unsubscribe from non existing mailing list ID %id', array(
      '%id' => $tid,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  if ($confirm) {

    // Make sure the mail address is set.
    if (empty($subscriber)) {
      $subscriber = new stdClass();
      $subscriber->mail = $mail;
    }
    simplenews_confirmation_send('unsubscribe', $subscriber, simplenews_category_load($tid));
  }
  elseif (isset($subscriber->tids[$tid])) {

    // Unsubscribe the user from the mailing list.
    $subscriber->newsletter_subscription[$tid]->status = SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED;
    $subscriber->newsletter_subscription[$tid]->timestamp = REQUEST_TIME;
    $subscriber->newsletter_subscription[$tid]->source = $source;
    simplenews_subscription_save($subscriber->newsletter_subscription[$tid]);
    unset($subscriber->tids[$tid]);

    // Clear eventually existing mail spool rows for this subscriber.
    module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
    simplenews_delete_spool(array(
      'snid' => $subscriber->snid,
      'tid' => $tid,
    ));
    module_invoke_all('simplenews_unsubscribe_user', $subscriber, $subscriber->newsletter_subscription[$tid]);
  }
  return TRUE;
}