How to send emails based on user role?

Created on 20 July 2023, over 1 year ago

Problem/Motivation

I am trying to reproduce some custom code from a Drupal 7 site which is being migrated to D9, using Rules.

The functionality I'm trying to reproduce is:

A Website Admin saves a new instance of an Announcement content type, which includes selecting which user roles will be sent an email notification of the body of the Announcement. This selection would be in the form of a checkbox on the content form and if it is too complex to have 1 Rule for this, I'd make a Rule for each type of role.

My issue is that I can't understand how to select which user roles should receive the email. The simple version of I'm trying is:

Events
After saving a new content item of type Announcement
Machine name: rules_entity_insert:node--announcement

Condition
Entity is of bundle
Parameters: entity: node, type: node, bundle: announcement
Node is published
Parameters: node: node

Actions
Add a variable
Parameters: type: entity:user_role, value: test_role

Send email to all users of a role
Parameters: subject: node.title.value, message: node.body.value, roles: [test_role], reply: '', language: ''

At first I just used "Send email to all users of a role" but was getting the error listed here . I also tried adding the variable (test_role is a role that does exist) but nothing seemed to work for the Data Selector or Direct Input.

There are other questions similar to mine but are asking how to get the current user's role, not a role specified by the user, which is what I would like.

I can add more information but I think my question boils down to: how do I specify which role emails will be sent to using the 'Send email to all users of a role' action?

Thank you.

uuid: e626380e-dc38-472d-8c19-bdd7227e3592
langcode: en
status: false
dependencies: {  }
id: send_member_email_announcement_
label: 'Send Member Email (Announcement) '
events:
  -
    event_name: 'rules_entity_insert:node--announcement'
description: 'Sends the Member Email (Announcement) after publishing the content'
tags: {  }
config_version: '3'
expression:
  id: rules_rule
  uuid: 2bd627cc-f0bd-4a7c-ab0f-d378ec47a805
  weight: 0
  conditions:
    id: rules_and
    uuid: 5caf29cb-7264-4f59-9f36-3786b56fa417
    weight: 0
    conditions:
      -
        id: rules_condition
        uuid: 7a5a9db1-134a-4f3a-acfd-c53c5a69234a
        weight: -50
        condition_id: rules_entity_is_of_bundle
        negate: false
        context_values:
          type: node
          bundle: announcement
        context_mapping:
          entity: node
        context_processors:
          type:
            rules_tokens: {  }
          bundle:
            rules_tokens: {  }
        provides_mapping: {  }
      -
        id: rules_condition
        uuid: d9aa0be1-98fb-4b3f-acb9-ef7756cca4b1
        weight: -49
        condition_id: rules_node_is_published
        negate: false
        context_values: {  }
        context_mapping:
          node: node
        context_processors: {  }
        provides_mapping: {  }
  actions:
    id: rules_action_set
    uuid: b117e896-f876-4736-8d1b-67610b482b14
    weight: 0
    actions:
      -
        id: rules_action
        uuid: 16635b6b-1a67-4ced-8d91-1ef52b828a0a
        weight: -50
        action_id: rules_variable_add
        context_values:
          type: 'entity:user_role'
          value: test_role
        context_mapping: {  }
        context_processors:
          type:
            rules_tokens: {  }
          value:
            rules_tokens: {  }
        provides_mapping: {  }
      -
        id: rules_action
        uuid: a4b2fac0-adef-4e6d-990e-c8a72a803fad
        weight: -49
        action_id: rules_email_to_users_of_role
        context_values:
          roles:
            - test_role
          reply: ''
          language: ''
        context_mapping:
          subject: node.title.value
          message: node.body.value
        context_processors:
          roles:
            rules_tokens: {  }
          reply:
            rules_tokens: {  }
          language:
            rules_tokens: {  }
        provides_mapping: {  }

💬 Support request
Status

Active

Component

Actions

Created by

🇳🇿New Zealand dominictaylor

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024