function rules_action_mail_to_users_of_role

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

Related topics

1 string reference to 'rules_action_mail_to_users_of_role'
rules_system_action_info in modules/system.rules.inc
Implements hook_rules_action_info() on behalf of the system module.

File

modules/system.eval.inc, line 115

Code

function rules_action_mail_to_users_of_role($roles, $subject, $message, $from, $settings, RulesState $state, RulesPlugin $element) {
    $from = !empty($from) ? str_replace(array(
        "\r",
        "\n",
    ), '', $from) : NULL;
    // All authenticated users, which is everybody.
    if (in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
        $result = db_query('SELECT mail FROM {users} WHERE uid > 0');
    }
    else {
        // Avoid sending emails to members of two or more target role groups.
        $result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN (:rids)', array(
            ':rids' => $roles,
        ));
    }
    // Now, actually send the mails.
    $params = array(
        'subject' => $subject,
        'message' => $message,
    );
    // Set a unique key for this mail.
    $name = isset($element->root()->name) ? $element->root()->name : 'unnamed';
    $key = 'rules_action_mail_to_users_of_role_' . $name . '_' . $element->elementId();
    $languages = language_list();
    $message = array(
        'result' => TRUE,
    );
    foreach ($result as $row) {
        $message = drupal_mail('rules', $key, $row->mail, language_default(), $params, $from);
        // If $message['result'] is FALSE, then it's likely that email sending is
        // failing at the moment, and we should just abort sending any more. If
        // however, $message['result'] is NULL, then it's likely that a module has
        // aborted sending this particular email to this particular user, and we
        // should just keep on sending emails to the other users.
        // For more information on the result value, see drupal_mail().
        if ($message['result'] === FALSE) {
            break;
        }
    }
    if ($message['result'] !== FALSE) {
        $role_names = array_intersect_key(user_roles(TRUE), array_flip($roles));
        watchdog('rules', 'Successfully sent email to the role(s) %roles.', array(
            '%roles' => implode(', ', $role_names),
        ));
    }
}