Get notified of new comments with Rules

Published March 10, 2012

This tutorial is specific to Drupal 7 and may not be applicable to the Drupal 8 version of the Rules module.

I know there are modules to handle this, but Rules can do it all:

If you visit admin > config > workflow > rules on your site, start a new Rule like so:

Name: "Notify of new comment"

React on Event: After saving a new comment

I normally tag the rule with the name of the site, like "drupaleasy".

Next add a new Condition:

Select the condition to add: User > User has role(s)

User > Data selector: "comment:author"

Roles > Value: administrator

check the Negate box

This looks at the role of the person who just left the new comment. If he or she is an administrator, this rule wil not fire. Chances are, if an admin left a comment, you don't want to know about it. On most sites I don't add this condition, but on a single-user site like a blog, this rule will make a lot of sense.

Next add an Action - this is what you want to happen when a new comment is created (that was not added by the administrator):

System: Send mail

To > Value: "[site:mail]"

Subject > Value: "New comment on [comment:node]"

Message > Value:

[site:url]admin/content/comment/approval

[comment:url]

[comment:title] - [comment:node]

at [comment:created] by [comment:author]

[comment:mail] - [comment:homepage]

[comment:body]

[comment:edit-url]

From > Value: "[comment:mail]"

On my sites, Anonymous comments have to pass approval - we just get too much spam otherwise. Luckily, now that we have this rule in place, we can think about lifting the approval gate so comments appear instantaneously while still helping us cut down on spam.

Here is an exported version of the rule:

{ "rules_notify_of_new_comments" : {
    "LABEL" : "Notify of new Comments",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "drupaleasy" ],
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : [ "comment_insert" ],
    "IF" : [
      { "NOT user_has_role" : {
          "account" : [ "comment:author" ],
          "roles" : { "value" : { "3" : "3" } }
        }
      }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[site:mail]",
          "subject" : "New comment on [comment:node:title]",
          "message" : "[site:url]admin\/content\/comment\/approval\r\n[comment:url]\r\n\r\n[comment:title]\t - [comment:node]\r\nat [comment:created] by [comment:author]\r\n[comment:mail] - [comment:homepage]\r\n\r\n[comment:body]\r\n\r\n[comment:edit-url]",
          "from" : "[comment:mail]"
        }
      }
    ]
  }
}

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!

Name
CAPTCHA