tags:

views:

14

answers:

1

Hi,

There is a checkbox added in user profile form, subscribe to daily newsletter. I want to get list of users who have subscribed (checkbox ON) and send them daily newsletter. How can I do this in drupal-6. (1) to get list of all subscribed users (2) Send them Email.

If you can explain at code level, I 'll really appreciate that.

+1  A: 

If your profile field is profile_newsletter, your :

<?php
  function mymodule_get_subscribers() {
    $query = 'select * from {profile_fields} where `name`="%s";';
    $result = db_query($query,'profile_newsletter');

    $field = db_fetch_object($result);

    $user_query = 'select * from {profile_values} where `fid`=%d and `value`=1;';
    $result = db_query($user_query,$field->fid);
    $subscribers = array();

    while($subscriber = db_fetch_object($result)) {
      $subscribers[] = $subscriber->uid;
    }

    return $subscribers; // Your array of subscriber user IDs
  }
?>

This code was quick, is untested and should probably contain a few sanity-checks. But it should work.

For the sending of newsletters and such I'd recommend using a pre-rolled module. I haven't tried any for Drupal, but Simplenews seems to do the trick. http://drupal.org/project/simplenews

If nothing else it probably contains a good sending-function. Otherwise just use PHP mail()-function.

jbn
really great help ... tons of thanks :)
dev

related questions