Email notification for user role change

Hi,
Is it possible to trigger an email notification to a user to let them know their role has been changed? I am setting up a site with an applicant / participant role structure, where the admin changes the role to participant once the applicant has been approved. It would be great if this triggered an automatic notification to the user.

Sorry, I might be spamming the forum a bit while working out the ins and outs of Kirby, but really enjoying it so far!

Hooks to the rescue, in this case:

No worries, we are here to help.

Perfect - thanks again!

Hi,

I reopened this topic because unfortunately I am quite ignorant about the use of hooks, sorry.

my problem is the same as mikeharrison, but I don’t know what to put inside the hook user.changeRole:after

I thought something like this in config.php, but it’s definitely wrong. Can someone help me?

return [
‘hooks’ => [

'user.changeRole:after' => function (Kirby\Cms\User $newUser, Kirby\Cms\User $oldUser) {
  try {
      $kirby->email([
          'template' => 'changerole',
          'from'     => 'noreply@test.com',
          'to'       => $newUser['email'],
          'subject'  => 'your role has changed',
      ]);
  } catch (Exception $error) {
    echo $error;
  }    
}

],
];

$newUser is not an array but an object which you get with the email method

$newUser->email()

Thank you for your reply,
I modified according to your instructions but it still doesn’t work.

When I change role, an error appears (screenshot)

My code:

  'hooks' => [
    'user.changeRole:after' => function (Kirby\Cms\User $newUser, Kirby\Cms\User $oldUser,) {

      try {

        $kirby->email([
          'template' => 'changerole',
          'from'     => 'noreply@test.com',
          'to'       => $newUser->email(),
          'subject'  => 'your role has changed',
        ]);
      } catch (Exception $error) {
        echo $error;
      }
    }
  ],

$kirby is not defined. Use kirby() instead

It works! :heart: