You are here

function rules_action_mimemail_to_users_of_role in Mime Mail 7

Action: Send HTML mail to all users of a specific role group(s).

1 string reference to 'rules_action_mimemail_to_users_of_role'
mimemail_rules_action_info in ./mimemail.rules.inc
Implements hook_rules_action_info().

File

./mimemail.rules.inc, line 318
Rules actions for sending Mime-encoded emails.

Code

function rules_action_mimemail_to_users_of_role($key, $roles, $active, $from_name = NULL, $from_mail = NULL, $reply_to = NULL, $subject, $body, $plaintext = NULL, $attachments = array(), $use_userlang = FALSE, $langcode = NULL, $settings, RulesState $state, RulesPlugin $element) {
  module_load_include('inc', 'mimemail');

  // Set the sender name and from address.
  if (empty($from_mail)) {
    $from = NULL;
  }
  else {
    $from = array(
      'name' => $from_name,
      'mail' => $from_mail,
    );

    // Create an address string.
    $from = mimemail_address($from);
  }
  $query = db_select('users', 'u');
  $query
    ->fields('u', array(
    'mail',
    'language',
  ));
  if ($active) {
    $query
      ->condition('u.status', 1, '=');
  }
  if (in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
    $query
      ->condition('u.uid', 0, '>');
  }
  else {
    $query
      ->join('users_roles', 'r', 'u.uid = r.uid');
    $query
      ->condition('r.rid', $roles, 'IN');
    $query
      ->distinct();
  }
  $result = $query
    ->execute();
  $params = array(
    'context' => array(
      'subject' => $subject,
      'body' => $body,
      'action' => $element,
      'state' => $state,
    ),
    'reply-to' => $reply_to,
    'plaintext' => $plaintext,
    'attachments' => $attachments,
  );

  // Create language list before initializing foreach.
  $languages = language_list();
  $message = array(
    'result' => TRUE,
  );
  foreach ($result as $row) {

    // Decide which language to use.
    if (!$use_userlang || empty($row->language) || !isset($languages[$row->language])) {
      $language = isset($languages[$langcode]) ? $languages[$langcode] : language_default();
    }
    else {
      $language = $languages[$row->language];
    }
    $message = drupal_mail('mimemail', $key, $row->mail, $language, $params, $from);
    if (!$message['result']) {
      break;
    }
  }
  if ($message['result']) {
    $role_names = array_intersect_key(user_roles(TRUE), array_flip($roles));
    watchdog('rules', 'Successfully sent HTML email to the role(s) %roles.', array(
      '%roles' => implode(', ', $role_names),
    ));
  }
  return array(
    'send_status' => !empty($message['result']),
  );
}