Trevor Twining originally posted code for a module that automatically subscribed users to a specified newsletter on login - http://drupal.org/node/56368#comment-118414. I've modified Trevor's code to work with Drupal 5 and the more recent version of SimpleNews. Please correct any mistakes I may have made, as this is my first module posting! Here's the code for the info file, which of course is now required for the module to be seen by Drupal 5:

aic_mailer.info

; $Id $ 
name = Simplenews auto-subscribe
description = Automatically subscribe users logging in to the site newsletter.
package = Simplenews

and here's the module code:

aic_mailer.module

<?php
/**
 * Implementation of hook_user().
 *
 */
function aic_mailer_user($op, &$edit, &$account, $category = NULL) {
	global $user;
	switch ($op):
		case "login":
			aic_mailer_check_subscriptions($user);
			break;
		case "logout":
			aic_mailer_check_subscriptions($user);
			break;	
	endswitch;
}

function aic_mailer_check_subscriptions($user, $debug='false') {
	if($user && $user->uid > 0){
		/* Change this number to your newsletter ID */
		$newsletter = 13;
		$sql = "SELECT * FROM {simplenews_subscriptions} a, {simplenews_snid_tid} b WHERE a.uid={$user->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={$user->uid}"));
			db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $sub->snid, $newsletter);
		}
	}
}
?>

Feedback welcome :)

Comments

wayfarer_boy’s picture

Many apologies - I'd not read the simplenews.module file properly. If I had, I would have seen the one function required to make this module work seamlessly. So:

aic_mailer.module

<?php
/**
 * Implementation of hook_user().
 *
 */
function aic_mailer_user($op, &$edit, &$account, $category = NULL) {
	global $user;
	switch ($op):
		case "login":
			aic_mailer_check_subscriptions($user);
			break;
		case "logout":
			aic_mailer_check_subscriptions($user);
			break;	
	endswitch;
}

function aic_mailer_check_subscriptions($user, $debug='false') {
	if($user && $user->uid > 0){
		/* Change this number to your newsletter's tid */
		$newsletter = 13;
		simplenews_subscribe_user($user->mail, $newsletter, $confirm = FALSE);
	}
}
?>

And that's all it needs to be.

sessy’s picture

Or you could disable the permission to subscribe/unsubscribe, and add this to a module:

function MODULENAME_user($op, &$edit, &$account, $category = NULL) {
    if($op == 'insert') {
        $newsletter = variable_get('force-this-newsletter', 2);
        simplenews_subscribe_user($account->mail, $newsletter, $confirm = FALSE);
    }
}

This will subscribe all new users to the selected newsletter.

yurtboy’s picture

works well for me..

codenamerhubarb’s picture

Works for me too (using Drupal 5). Thanks.

-------------------------------
My Drupal site which I love to pieces: Download a Book.

-------------------------------
My Drupal site: Download a Book.

grendzy’s picture

I tested sessy's method in Drupal 6, and it works well.

stanford-1’s picture

This worked great! Thanks!

kiova’s picture

possible to use this code for multiple newsletters?

dbetschart’s picture

thanks for this, but how can i get the tid of my newsletter? i can't see the tid of my newsletter anywhere, where can i get it?
thanks!

vood002’s picture

There is a module now that does this http://drupal.org/project/simplenews_register

Jason Brain’s picture

I'm interested in applying this on a v6 installation.

Anyone have much luck?
What mods are required for Sessy's solution?