You are here

function simplenews_unsubscribe in Simplenews 7.2

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.

$newsletter_id: The newsletter ID.

$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

12 calls to simplenews_unsubscribe()
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_combined in includes/simplenews.subscription.inc
Menu callback: confirm a combined confirmation request.
simplenews_confirm_multi_form_submit in includes/simplenews.subscription.inc

... See full list

File

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

Code

function simplenews_unsubscribe($mail, $newsletter_id, $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 (!($newsletter = simplenews_newsletter_load($newsletter_id))) {
    watchdog('simplenews', 'Attempt to unsubscribe from non existing mailing list ID %id', array(
      '%id' => $newsletter_id,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  if ($confirm) {

    // Make sure the mail address is set.
    if (empty($subscriber)) {
      $subscriber = entity_create('simplenews_subscriber', array());
      $subscriber->mail = $mail;
    }
    simplenews_confirmation_send('unsubscribe', $subscriber, simplenews_newsletter_load($newsletter_id));
  }
  elseif (isset($subscriber->newsletter_ids[$newsletter_id])) {

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

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