Send an email to everyone in a role group

Note that action snippets need to be contained in a module for the task. Make sure you read http://drupal.org/node/48737.

This action will send an email to everyone user in the selected role groups. This is useful for workflow changes to alert editors that they have nodes to review.

<?php
function action_send_email_torolegroup($op, $edit = array(), $node) {
  switch(
$op) {
    case
'metadata':
      return array(
       
'description' => t('Send Email to a role group'),
       
'type' => t('Email'),
       
'batchable' => false,
       
'configurable' => true,
      );

    case
'do':
     
// note this is the user who owns the node, not global $user
     
$user = user_load(array('uid' => $node->uid));
     
$site_name = variable_get('site_name', 'Drupal');
     
$from = "$site_name <" . variable_get('site_mail', ini_get('sendmail_from')) . '>';
     
$subject = $edit['subject'];
     
$message = $edit['message'];
     
      foreach(
$edit['recipients'] as $rid => $chose_role) {
        if (
$chose_role) {
         
$recipient_roles[] = $rid;
        }
       
      }
     
     
$recipient_addresses = array();
     
      if (
$recipient_roles[2]) { //email every one! wow. that's why a watchdog is invoked

       
watchdog('action', t('Sent an email to every single registered user. Use with caution.'));
       
       
$result = db_query('SELECT mail FROM {users} WHERE uid > 0');
        while(
$account = db_fetch_object($result)) {
         
$recipient_addresses[] = $account->mail;
        }
      } else {
       
$roles = implode(',', $recipient_roles);    
       
$result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN (%s)', $roles);
        while(
$account = db_fetch_object($result)) {
         
$recipient_addresses[] = $account->mail;
        }
      }
     
      if (isset(
$node) && is_object($node)) {
       
$variables = array(
         
'%site_name' => $site_name,
         
'%username' => $user->name,
         
'%uid' => $node->uid,
         
'%node_url' => url('node/' . $node->nid, NULL, NULL, TRUE),
         
'%node_type' => $node->type,
         
'%title' => $node->title,
         
'%teaser' => strip_tags($node->teaser),
         
'%body' => strip_tags($node->body)
           );

       
$message = strtr($message, $variables);
      }
      foreach (
$recipient_addresses as $recipient) {
        if (
user_mail($recipient, $subject, $message, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from" )) {
         
watchdog('action', t('Sent email to %recipient', array('%recipient' => $recipient)));
        }
        else {
         
watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $recipient)));
        }
      }
      break;

   
// return an HTML config form for the action
   
case 'form':
     
// default values for form
      //  if (!isset($edit['recipients'])) $edit['recipients'] = '';
     
if (!isset($edit['subject'])) $edit['subject'] = '';
      if (!isset(
$edit['message'])) $edit['message'] = '';
     
$form = array();
     
$roles = user_roles();
      unset(
$roles[1]); // good bye anonymous users!
      //unset($roles[2]); // good bye authenticated users!
           
     
$form['recipients'] = array(
       
'#type' => 'checkboxes',
       
'#title' => t('Recipient Role Groups'),
       
'#default_value' => $edit['recipients'],
       
'#options' => $roles,
       
'#description' => t('Select which roles should receive this email.'),
    );
     
     
$form['subject'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Subject'),
       
'#default_value' => $edit['subject'],
       
'#size' => '20',
       
'#maxlength' => '254',
       
'#description' => t('The subject of the message.'),
      );
     
$form['message'] = array(
       
'#type' => 'textarea',
       
'#title' => t('Message'),
       
'#default_value' => $edit['message'],
       
'#cols' => '80',
       
'#rows' => '20',
       
'#description' => t('The message that should be sent.  You may include the following variables: %site_name, %username, %node_url, %node_type, %title, %teaser, %body'),
      );
      return
$form;

    
// validate the HTML form
   
case 'validate':
     
$errors = array();
     
     
$roleselected = false;
      foreach(
$edit['recipients'] as $rid => $selected) {
        if (
$selected) {
         
$roleselected = true;
        }
      }
      if (!
$roleselected) {
       
$errors['recipients'] = t('Please enter at least one recipient role group');
      }
     
      foreach (
$errors as $name => $message) {
       
form_set_error($name, $message);
      }

      return
count($errors) == 0;

   
// process the HTML form to store configuration
   
case 'submit':

     
$params = array(
       
'recipients' => $edit['recipients'],
       
'subject'   => $edit['subject'],
       
'message'   => $edit['message']);
      return
$params;
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.