Users who are not subscribing to any newsletters are deleted from subscriptions table. If importing subscribers from an external source (like a customer database), this might result in users being added to the subscription list, even if they've once declined subscriptions once. I changed the following to achieve this:

function simplenews_unsubscribe_user from:

    // Clean up simplenews_subscriptions if no more newsletter subscriptions.
    if (!db_num_rows(db_query("SELECT tid FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) {
      db_query('DELETE FROM {simplenews_subscriptions} WHERE snid = %d', $subscription->snid);
    }
  }

to:

    // Clean up simplenews_subscriptions if no more newsletter subscriptions.
    if (!db_num_rows(db_query("SELECT tid FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) {
      db_query('UPDATE {simplenews_subscriptions} SET a_status=0 WHERE snid = %d', $subscription->snid);
    }
  }

function simplenews_subscribe_user from:

  if ($confirm) {
    // Send confirmation e-mail to user to complete subscription or to tell
    // them that he or she is already subscribed.
    simplenews_mail_confirm($mail, $newsletter, $subscription ? $subscription->snid : NULL, 'subscribe');
  }
  elseif (!isset($subscription->tids[$tid])) { 
    // Then, add user to newsletter relationship if not already subscribed.
    db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $subscription->snid, $tid);
  }

to:

  if ($confirm) {
    // Send confirmation e-mail to user to complete subscription or to tell
    // them that he or she is already subscribed.
    simplenews_mail_confirm($mail, $newsletter, $subscription ? $subscription->snid : NULL, 'subscribe');
  }
  elseif (!isset($subscription->tids[$tid]) && $subscription->a_status > 0) { //inactive subscribers will not be added to newsletter
    // Then, add user to newsletter relationship if not already subscribed.
    db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $subscription->snid, $tid);
  }

Comments

emdalton’s picture

Thank you! This is exactly what I needed!

I'll test and post results here.

emdalton’s picture

The above change works, as far as not re-adding people who were previously subscribed, even if they have subsequently unsubscribed. However, this highlights an issue with the existing code. If you attempt to add a list of email addresses to a newsletter and one of the addresses is already subscribed to a different newsletter in Simplenews, a message indicates that the email address has been added, but in fact it is not added. This is a bit misleading.

I would still like to see this function rolled into the production version of Simplenews, possibly with a switch so administrators can decide whether they want this functionality (which effectively "inactivates" addresses, rather than deleting them) or want unsubscribed addresses to be removed from the database.

sutharsan’s picture

I like to see this function in 6.x, but don't plan to add any new features in 5 anymore.
Anyone who like to port this to 6?

adamo’s picture

I ported the above code to Simplenews 6.x-1.0-rc4, but there are multiple problems with this.

For one, subscribers that do not have any subscriptions do not show up in the list of subscriptions. They would be in the DB, but there would be no way for a end user to know that. Number two (even worse), this makes it impossible for the subscriber to resubscribe to any newsletter. They would get a message saying they were subscribed but they would not actually be subscribed. Since subscribers without subscriptions don't appear in the subscribers list, there is no way to make them active again without manually editing the DB. A better way of doing this might be to log imported subscribers to a separate table, and then have the import code check records to be imported against that table and only import records that have not previously been imported.

I included the code below in case it might be useful to anyone but I would NOT recommend using it. :/

function simplenews_unsubscribe_user from:

<?php
    // Clean up subscription account if user is not subscribed to any newsletter anymore
    if (!db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) {
      db_query('DELETE FROM {simplenews_subscriptions} WHERE snid = %d', $subscription->snid);
    }
?>

to:

<?php
    // Clean up subscription account if user is not subscribed to any newsletter anymore
    if (!db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) {
      db_query('UPDATE {simplenews_subscriptions} SET activated=0 WHERE snid = %d', $subscription->snid);
    }
?>

function simplenews_subscribe_user from:

<?php
  elseif (!isset($subscription->tids[$tid])) {
    // OR add user to newsletter relationship if not already subscribed.
    db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $subscription->snid, $tid);

    // Execute simplenews subscribe trigger.
    simplenews_call_actions('subscribe', $subscription);
  }
?>

to:

<?php
elseif (!isset($subscription->tids[$tid]) && $subscription->activated > 0) {
    // OR add user to newsletter relationship if not already subscribed and subscriber not deactivated.
    db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $subscription->snid, $tid);

    // Execute simplenews subscribe trigger.
    simplenews_call_actions('subscribe', $subscription);
  }
?>
sutharsan’s picture

Version: 5.x-1.1 » 7.x-1.x-dev
Status: Active » Needs work

Changing the version to HEAD. This is where all new features will go.

Unsubscribers should be able to re-subscribe later. I may require some database change. In addition to this I like to see added a timestamps for (un)subscription and source fields for the (un)subscription source (e.g imported, website or services).

@adamo: I would appreciate it if you could write code for this.

adamo’s picture

I would love to help out, but honestly I've just started tinkering with Drupal development and I'm probably not the best person for this job right now. I'll come back to this when I'm a little more familiar with how all this stuff works (assuming nobody else tackles it in the meantime).

sutharsan’s picture

I'll be happy to help you, if you want to dedicate your time to this. Just let me know what you need to get started. This is not a too dificult job to get started.

sutharsan’s picture

Status: Needs work » Closed (duplicate)

This is realized in 6.x-2.x branch.