In this video, I will guide you on how to create a new rule that will send an email notification to the author of an article when a new comment is posted.
- Go to “Configuration => Workflow => Rules” (admin/config/workflow/rules)
This pages lists all active and inactive rules. I don’t have any yet, so I’ll go ahead and add one now.
- Click "Add new rule"
- Name: Send Email Notification on New Comment (it will create a machine name for you, you can edit this by clicking "Edit")
- Tags: I won’t include any tags. They are used in the admin interface for filtering when you have a lot of Rules, but since I’m only going to have a few, I won’t bother.
- React on event: After saving a new comment (Rules provides a number of events, in this video we want to send an email when a new comment is posted, so I’m going to choose "After saving a new comment")
Now, we’re taken to the configuration page for this rule. It’s broken into three sections: Events, Conditions and Actions. Events can be anything from “when a node is saved” to “when a user logs in”. Conditions limit when a rule should be executed. Conditions include things like “Node is of type (x)” and “User has role (x)”. Finally, actions are what should be executed. Actions include things like “Promote content to frontpage” and “Show a message on the site”.
The event “After saving a new comment” that we chose, is already in the “Event” section. Since we’re going to notify the node author when their node gets a comment, they probably don’t need to be notified when they leave a comment. So, let’s add a condition that says will only let this rule fire when the comment author is not the same as the node author.
- Click "Add condition"
- Select the condition to add: Data comparison (We are going to compare two user objects, which are comment author and node author)
- Click "Continue"
- Data to compare: comment:author (The data selection input method allows you to drill down into the data available to Rules, in that particular context, so when I selected comment ending with ':' it let me access data related that comment.)
- Click "Continue"
- Value for operator "equals" (We’re going to negate this in a minute)
- Data Value: comment:node:author (If you hover over “comment:node” you’ll see a text popup that says “The node the comment was posted to”, then we can select the author of that node.)
- Check "Negate" (This means that instead of making sure the two values equal each other, we’ll make sure that they do not equal each other.)
- Click "Save"
Now, we’re ready to add the action, which will be to email the node author about the comment.
- Click "Add action"
- Select "Send mail" (after saving a new comment, an email will be sent)
- To: [comment:node:author:mail] (You can simply type in any valid email address like for example “email@example.com", but what we want is to send an email to the author of the node, so I'll go ahead and click "Switch to data selection" and choose "[comment:node:author:mail]")
- Subject: Comment on [comment:node] (This time, I’ll not switch to data selection so that I can enter additional text. We can expand the “Replacement patterns” dropdown to get the comment url, body etc.)
View the comment at [comment:url]
Posted By: [comment:name]
- From: [comment:mail] (Switch to the direct input mode) (This will use the email address of the user if they are logged in. The site-wide email address will be used for anonymous comments.)
- Language: (I’ll leave this blank)