Drupal Simplenews: Automatically Subscribe New Users to a Newsletter
This post is more than 10 years old. I do not delete posts, because even old information is still useful, but please know that some material on this page may be outdated or incorrect. Thanks!
One of the sites I am setting up requires that all users be subscribed to a certain newsletter (or maybe two, depending on who they are) via Simplenews when they create their accounts (actually, their accounts are automatically created via LDAP... but that's another story).
Looking around, I found this post explaining how you might be able to auto-subscribe new users, and it led me to look up Simplenews' simplenews_subscribe_user() function.
Basically, you can add a line like the following in your custom module's hook_user() function, on the 'insert' $op (for Drupal 6):
<?php
// simplenews_subscribe_user(<email>, <tid_of_newsletter>, <send_confirmation_email_boolean>, <source_of_subscription>, <language>)
simplenews_subscribe_user($account->mail, 45, $confirm = FALSE, $source = 'action', NULL);
?>
</code><p>Additionally, you can subscribe ALL of your site's users (or whatever subset you choose, via your own logic) to a Simplenews newsletter using the following PHP snippet (you can just paste this in <a href="http://drupal.org/project/devel">Devel</a>'s 'Execute PHP Code' block):</p>
<code>
<?php
$uids_to_update = array(0, 1, 2);
foreach ($uids_to_update as $uid) {
$account = user_load($uid);
simplenews_subscribe_user($account->mail, 45, $confirm = FALSE, $source = 'action', NULL);
}
?>
I grabbed a list of all the active uids on the site using views, turned them into an array (used quick regex in TextMate to turn a line-broken list into value, value, value...), then used the code above (with my own array in it) to subscribe all active users to the newsletter.
Comments