Send an email when the roles are changed I'm trying to send an email to users after their role has changed. So far, the rule is triggered every time the profile is saved (even without role change); I suspect that the data comparison is not handling the array correctly?!
Any help to improve the following rule or to achieve the same feature with a different condition will be very much appreciated.
{ &quot;rules_role_changed&quot; : {
    &quot;LABEL&quot; : &quot;Role changed&quot;,
    &quot;PLUGIN&quot; : &quot;reaction rule&quot;,
    &quot;OWNER&quot; : &quot;rules&quot;,
    &quot;REQUIRES&quot; : [ &quot;rules&quot; ],
    &quot;ON&quot; : { &quot;user_update&quot; : [] },
    &quot;IF&quot; : [
      { &quot;NOT data_is&quot; : { &quot;data&quot; : [ &quot;account:roles&quot; ], &quot;value&quot; : [ &quot;account-unchanged:roles&quot; ] } }
    ],
    &quot;DO&quot; : [
      { &quot;mail&quot; : {
          &quot;to&quot; : [ &quot;account:mail&quot; ],
          &quot;subject&quot; : &quot;Role change for [account:name-raw]&quot;
        }
      }
    ]
  }
}
To detect 'any' change to the users roles, you can use the following PHP via the condition "Use PHP code"

return (count(array_diff_key($account-&gt;roles, $account_unchanged-&gt;roles)) + count(array_diff_key($account_unchanged-&gt;roles, $account-&gt;roles))) &lt;&gt; 0;


The other PHP answers will not necessarily fire when a user has been 'removed' from a role.