If we want to send e-mails to individual users, it's better to use the Send mail Action in our rule configuration. A good use case could be when we want to notify a node's author when someone leaves a comment on a node created by them. We could then use the Event Comment | After saving a new comment, then add two Actions.
The following steps will help you send notifications if a user comments on a node created by another user:
In the first Action, we need to load the author of the node that's being commented on. For that we can use Data | Add a variable Action and set the value of TYPE to User:
We then set the value to the node's author.
Optionally, we can set the label of the variable, so we can easily identify it in the next step:
We add another Action, System | Send mail and use REPLACEMENT PATTERNS to make use of the variable we added in the previous step.
We can then use node-author:mail in the To field which is the e-mail address of the original node author.
If we don't want the author to be notified of their own comments, we can add a Condition, Data | Data comparison and set the DATA TO COMPARE value to the user ID of the node author, as shown in the following screenshot:
Next, we hit Continue and set the data value to the user ID of the comment author, as shown in the following screenshot:
This configuration is of course incorrect at this stage, as Rules would only send an e-mail if the Node author and the Comment author are the same, which is the opposite of what we're looking for. A really good feature in Rules is Negate. This setting basically sets TRUE values to FALSE and vice versa, which is just what we currently need. The Negate feature is shown in the following screenshot:
This way the rule will only fire if the author of Node is not the same as the author of Comment.
The following section will throw light on how to send notifications only to users wanting to receive them.
For usability or to avoid our site users from feeling spammed, ideally we would want to add a new Boolean checkbox field to the user object that is used as a switch to indicate whether the user wants to receive these notifications from our site or not. Then in our rule configuration, we can use the Condition, Data | Data value is empty and set the Data selector value to that field. Assuming that the name of the field is field-notifications, it would look like the following screenshot:
This configuration is of course incorrect at this stage, as Rules would only send an e-mail if the field is empty (the user does not want notifications), which is the opposite of what we're looking for. So we need to use Negate .
This way the rule will only fire if that particular user has checked the Notifications field on his/her user account page.