Using Custom Tokens module to generate an array of email addresses

Last modified: April 16, 2009 - 16:59

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 Custom Tokens. This is an awesome way to quickly produce tokens without having to delve 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.

 
 

Drupal is a registered trademark of Dries Buytaert.