Community Documentation

Using Custom Tokens module to generate an array of email addresses

Last updated March 19, 2012. Created by andrewjsledge on April 16, 2009.
Edited by bfr, heshan.lk, hanoii. Log in to edit this page.

I had a need to create a list of email addresses from user accounts that belonged to a role. Of course, this had to be done in a programmatic way. After looking into the more common modules and trying my hand at writing my own tokens using the Token API, I found The Custom tokens module. This is an awesome way to quickly produce tokens without having to devel into writing your own token instances. For my needs, I was to create a list of email addresses so that I could use actions that allowed me to send tokenized emails.

Here is an example of how I accomplished this goal utilizing Custom Tokens.

Token ID: token_custom_role_editor
Description: Token for filling in an array for members of the editor role
Type: Global
PHP replacement:

<?php
$myRole
= "Editor";

$roleSQL = "SELECT * FROM {users} WHERE uid IN (SELECT uid FROM {users_roles} WHERE rid=(SELECT rid FROM {role} WHERE name='%s'))";
$roles = db_query($roleSQL, $myRole);
while(
$role = db_fetch_array($roles)) {
 
$usersinrole[] = $role['mail'];
}
if(
count($usersinrole) > 0) {
  print
implode(",",$usersinrole);
}
?>

Of course, it would not take too much effort to modify this to display or use other tables and their attributes.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Programmers, Site administrators
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.