I would like new registered user was automatically suscribed to my newsletter.

Some idea?

Thanks.

Comments

solanas’s picture

I would like to show a default checkbox for the suscription to one of my newsletters when the users register to my drupal site. I use simplenews. Anybody has some suggestion?

Thank you.

Marco Solanas

trevortwining’s picture

I had a similar problem, and created a module using user_hook with simplenews_process_subscription() that checks on login if existing users are subscribed to the default newsletter. I use the login operation to do the check, but you could just as easily put it in the insert, register, or save operations.

It's still very rough code right now, but it's doing the job I need it to do. These are the two main functions so far.


/**
 * Checks to see if all subscriptions are met
 *
 * @param object $user
   a valid user object
 */
function aic_mailer_check_subscriptions($user, $debug='false')
{
  if($user && $user->uid > 0){
    /*TODO
    IS THERE A BETTER QUERY TO USE?
    */
    $sql = "SELECT snid, s_status, mail, uid, a_status FROM {sn_subscriptions} WHERE uid={$user->uid}";
    
    $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.' );
      }
        //replace the 12 with the term_id of your newsletter
        simplenews_process_subscription(12, $user->mail, 'Subscribe');
    }
  }
}

/*implementation of hook_user()*/


function aic_mailer_user($type, $edit, &$user, $category = 'account'){
  
  switch ($type):
  case "login":
    aic_mailer_check_subscriptions($user);
    break;
  endswitch;
}

Trevor Twining

Trevor Twining
Freelance Drupal Dev
Theme/Modules/Sitebuilding

gafox’s picture

Trevor,

This is what I need. But . . . I'm a newbie at php. What do I need to do to actually implement this code, so it does what it's supposed to do? Can you spell out in more detail? I don't actually know what you mean by "created a module using user_hook with simplenews_process_subscription() " though the general idea is clear enough!

Thanks in advance!

Gordon

trevortwining’s picture

The first thing is to look at the module developer's guide.

http://drupal.org/node/508

If you're new to programming, and not just php, then a lot of this might seem completely foreign to you. That's okay: doesn't mean you shouldn't try. If I'm not explaining things clearly enough, don't be afraid to ask me to clarify; I'll do what I can to make this clear.

Drupal functionality is organized into modules. There are a number of core modules that form the main drupal installation. This is everything you get "out of the box." Then there are the user contributed modules that extend Drupal's functionality.

All of these programs have to follow a specific set of rules when they work with information in Drupal. This set of rules, when documented properly and published are what is known as an API (application programming interface).

Each module has certain 'hooks', or functions that allow programmers to access that particular module's feature set. In my example, I'm dealing with two modules: user.module and simplenews.module.

I could have gone in and changed the code manually in each of these modules to acheive the desired effect. But if a drupal upgrade were released then I would have to keep track of what I changed, as I would lose it when I overwrote my installation with updated files. I created my own module so that my code was kept separate from the others.

I need to use features from both simplenews and user. so the example code contains two functions. The first, aic_mailer_check_subscriptions takes a $user object (provided by Drupal through the user.module) and does some logic to see if they are subscribed, and if not calls the function from simplenews that registers a user to a list.

So now I have a function that does what I want, but how do I get it to run automatically?

I thought about where I could run this, and I figured that at login was the best way, because it means that this code is only run when that user logs in. In order to process this when the user logs in, I needed to hook into the functions of the user module. That's what the second function does. The only action I need to take is at login, which is why it's written the way it is, and then that hook calls the first function, which actually contains the instructions.

the function aic_mailer_user is special because of the '_user' at the end (the first part is the module name and is useful to avoid naming duplication -- very bad). That hook tells drupal that this function is extending, or 'hooking' into the main user function. That function has a several particular actions, and one of them is login. I have told it that on a regular user login, run the code that is contained within aic_mailer_check_subscriptions

I hope this helps you understand a bit more, it took me a while to understand how everything just kind of 'works' together. it seems simple at first, and then you dive in and feel overwhelmed with the complexity. But then you start to see the elegance of the system and you appreciate all the effort that went into it; you just wish that you could find more documentation or examples on how to do some of the more obscure things. I'm blown away by this project every day.

Trevor Twining
http://www.trevortwining.info

Trevor Twining
Freelance Drupal Dev
Theme/Modules/Sitebuilding

somebodysysop’s picture

OK, I'm going to try to implement your code. I know a little php and a little less about module development. Step by step:

1. I take your code and save it in my template.php file in my theme directory.

2. Next, in this code, I replace aic_mailer with the name of my theme.

OR

1. I take your code and save it as a module file (i.e., aic_mailer.module)

2. Enable the module in Drupal and that's it?

Finally

3. I would like to only require it at either the first login or when user signs up. Can you tell me what the replacement for "login" would be here:

function aic_mailer_user($type, $edit, &$user, $category = 'account'){
  
  switch ($type):
  case "login":
    aic_mailer_check_subscriptions($user);
    break;
  endswitch;
}

Am I on target here?

Thanks so much for taking the time to create this and explain how to use it.

-ron

<?php
/**
* Checks to see if all subscriptions are met
*
* @param object $user
  a valid user object
*/
function aic_mailer_check_subscriptions($user, $debug='false')
{
  if($user && $user->uid > 0){
    /*TODO
    IS THERE A BETTER QUERY TO USE?
    */
    $sql = "SELECT snid, s_status, mail, uid, a_status FROM {sn_subscriptions} WHERE uid={$user->uid}";
    
    $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.' );
      }
        //replace the 12 with the term_id of your newsletter
        simplenews_process_subscription(12, $user->mail, 'Subscribe');
    }
  }
}

/*implementation of hook_user()*/

function aic_mailer_user($type, $edit, &$user, $category = 'account'){
  
  switch ($type):
  case "login":
    aic_mailer_check_subscriptions($user);
    break;
  endswitch;
}
?>
trevortwining’s picture

The proper way to do it is create a folder inside the modules directory. Inside that folder, create a file called aic_mailer.module. Paste the code in.

I have no idea what would happen if you put that in the template structure, but my guess is some sort of include error would result.

If you want to do it when a user registers, then you can use 'insert' in case of 'login'. Warning here, I'm not sure if this would pass the proper $user object to the function above. You can do a little digging on this API page (http://api.drupal.org/api/4.6/function/hook_user) for the hook_user function.

Let me know if you have any other questions.

Trevor Twining
http://www.trevortwining.info

Trevor Twining
Freelance Drupal Dev
Theme/Modules/Sitebuilding

anthrocreative’s picture

For anyone trying to do this in Drupal 6, here's some cleaned-up code that hooks into functions provided by simplenews. I also altered it to check if the user is subscribed to a specific newsletter (the above code only checks to see if they're subscribed to ANY newsletter).

For newbies, I implemented this as a custom module.

<?php
/**
* Checks to see if mandatory subscriptions are in place
*
* @param object $user
   a valid user object
*/

function zha_announce_auto_subscribe_mailer_check_subscriptions($user, $debug='false') {
  if($user && $user->uid > 0){
        // replace 22 in the next two lines with the appropriate tid
  	if ($s = simplenews_user_is_subscribed($user->mail,22) == false) {
  		simplenews_subscribe_user($user->mail,22,false);
  	}
  } 
}

/*implementation of hook_user()*/

function zha_announce_auto_subscribe_user($type, $edit, &$user, $category = 'account'){
  
  switch ($type):
  case "login":
    zha_announce_auto_subscribe_mailer_check_subscriptions($user);
    break;
  endswitch;
}
?>
DrewDennis’s picture

Please clarify for me the tid? I assume that is a numeric id for the newsletter I wish to auto-sub the user to? How do I locate that number? after adding that in I save that code as a module and activate?

my-family’s picture

Thank you for the idea, it seems to work nice, but... after a certain user obtains a confirmation mail, the confirmation link goes to "page not found" page. Is there any solution? Thank you in advance.