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 = Simplenewsand 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
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
Or you could disable the permission to subscribe/unsubscribe, and add this to a module:
<?phpfunction 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...
works well for me..
That works
Works for me too (using Drupal 5). Thanks.
-------------------------------
My Drupal site which I love to pieces: Download a Book.
Works in D6 too
I tested sessy's method in Drupal 6, and it works well.
Thanks!
This worked great! Thanks!
is it
possible to use this code for multiple newsletters?
thanks for this, but how can
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
There is a module now that does this http://drupal.org/project/simplenews_register
----
http://www.johnpezzetti.com
v6 release?
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