Autosubscribe users to simplenews newsletter

wayfarer_boy - February 16, 2007 - 01:59

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 :)

Many apologies - I'd not

wayfarer_boy - February 16, 2007 - 15:07

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.

Alternative

sessy - June 4, 2007 - 20:47

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

<?php
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.

Thanks...

yurtboy - August 30, 2007 - 12:56

works well for me..

That works

codenamerhubarb - February 29, 2008 - 07:27

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

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

Works in D6 too

grendzy - August 22, 2008 - 20:09

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

Thanks!

stanford - December 20, 2008 - 22:49

This worked great! Thanks!

is it

baybara - June 18, 2007 - 09:49

possible to use this code for multiple newsletters?

thanks for this, but how can

dbetschart - August 11, 2009 - 15:19

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!

Module

vood002 - August 24, 2009 - 18:16

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

v6 release?

Jason Brain - December 8, 2009 - 01:37

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

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

Jason.Brain @ WeBeWho!com
IT, Web, Marketing and Promotional Solutions
Kawartha Lakes, Ontario

 
 

Drupal is a registered trademark of Dries Buytaert.