0

I'm about to lose it here so hopefully someone here can help me.

I'm working in D8 and I would like to allow users of a certain role (Managers) administrate new site users. There is a permission for that and it works fine, but I can't find a way to send an email to users of the Managers role. It seems that they will only send to the Administrator account (me).

Some people online suggested that I might use Rules to get this to work for me, but this has made things even more confusing. I started down the path with the event being "After saving new user". I click "Add Action" and pick "Send Email". Some screenshots have shown me that in previous versions there might be a "Send Email to Role" option, but that's gone here for some reason.

I'm now at a "Send To" field, which can use the Data Selector. I can't quite use the Data Selector to get to a role. I can choose "user.role" or "user.roles", but then the autocomplete kind of gives up on me. "user.roles" will start giving me numbers after or "entity", but nothing gets me to sending an email to all users with a specific role.

Does anyone know how to get this to work? I'm not married to the Rules module. It just seemed to be the only option because Drupal won't send the default emails to anyone that isn't Account 1.

  • piece of cake to get this to work, all that's needed is this. As an alternative, use D7. – Pierre.Vriens Jun 15 '17 at 21:24
0

I highly recommend you don't do this, but if you put the following code into a file rules/src/Plugin/RulesAction/SystemSendEmailToRole.php then it'll give you a new action where you can enter the machine name of the role you want to send to (e.g. "myadmins") and it'll do what you want (I tested it, it works though no money back & no guarantee!):

https://gist.github.com/stevepurkiss/9b36eb79311d16c65e8d4d0ba9f1854b

All I did was copy the send email action, swap the 'to' string field to 'role', then load up all user ids with that role, foreach loop through them and insert their email address one by one as it loops through.

Looks like it could be extended to cope with multiple roles like the original code copes with multiple email addresses but this is the best you get in the half hour since I saw your post, and for free ;)

Of course it'll break if you update rules, there's no guarantees, but I did a bit of Rules 7->8 upgrading actions a couple of years back so know my way around a little. Not a lot, but a little ;)

Please also consider supporting rules too, this stuff doesn't appear from thin air, it's a collaboration!

HTH

Steve

0

I managed to solve this with hook_mail_alter rather than using Rules. For posterity, here's my code:

function ptapp_mail_alter(&$message) {

  if($message['id'] == 'user_register_pending_approval_admin') {

    $query = \Drupal::entityQuery('user')
      ->condition('status', 1);

    $group = $query->orConditionGroup()
    ->condition('roles', 'manager')
    ->condition('roles', 'administrator');

    $ids = $query->condition($group)->execute();
    $users = User::loadMultiple($ids);

    foreach($users as $u) {
      $message['to'] .= ', '.$u->getEmail();
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.