Using Custom Tokens module to generate an array of email addresses

Last updated on
28 November 2024

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:


$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.

Help improve this page

Page status: Not set

You can: