Subscribe All Users to a Simplenews Newsletter

There's been a couple times where I've had to subscribe all users to a new newsletter.

I started out with a snippet I found which cleans up the subscriptions, but I lost the link or I would have referenced it here.

Here's the code that will subscribe all users to newsletter node id 16. Note I'm using mysql_insert_id() as a quick fix so you will need to use another method if you're not using mysql.

    $result = db_query("SELECT uid, mail FROM {users} WHERE uid>0");

    while ($row = db_fetch_object($result)) {

      /* Change this number to your newsletter ID */
      $newsletter = 16;
      $sql = "SELECT * FROM {simplenews_subscriptions} a, {simplenews_snid_tid} b WHERE a.uid={$row->uid} AND b.snid=a.snid AND b.tid=".$newsletter;

      $sub = db_fetch_object(db_query($sql));
      if (!$sub){
          if($debug == 'true'){
              drupal_set_message('no subscriptions for this user so we will be correcting that now.');
          }
          //$sub = db_fetch_object(db_query("SELECT snid FROM {simplenews_subscriptions} WHERE uid={$row->uid}"));

          db_query("INSERT INTO {simplenews_subscriptions} (a_status, s_status, mail, uid) VALUES(1, 0, '%s', %d)", $row->mail, $row->uid);
          $snid = mysql_insert_id();

          if ($snid) {
            db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $snid, $newsletter);
          }
      }

  }