6

I need to send an email when there is a new comment on a node to all users that have flagged that node? How do I do this?

The flag is called "Follow" and the content type is "Blog"

2 Answers 2

7

The exported code assumes use of Flag module to have a flag type as "follow" attached to Blog content type.

As a summary:

  • set rule event as "After saving a new comment of type Blog comment".
  • leave condition as none.
  • set actions as
    • step 1: get all users who have flagged "Fetch users who have flagged a Node".
    • step2: loop through the list.
      • step2.1: send email

Here is the export for my Rule configured as per your requirement.

{ "rules_email_on_blog_comment" : {
    "LABEL" : "Email On Blog Comment",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "blog", "email" ],
    "REQUIRES" : [ "flag", "rules", "comment" ],
    "ON" : { "comment_insert--comment_node_blog" : { "bundle" : "comment_node_blog" } },
    "DO" : [
      { "flag_fetch_users_node" : {
          "USING" : { "flag" : "follow", "node" : [ "comment:node" ] },
          "PROVIDE" : { "users" : { "users" : "Users who flagged" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "users" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "mail" : {
                "to" : "[list-item:mail]",
                "subject" : "New Comment",
                "message" : "There is a new comment on the flagged blog post",
                "from" : "[site:mail]",
                "language" : [ "" ]
              }
            }
          ]
        }
      }
    ]
  }
}
4
  • What do I use in the "to" field for the email? Commented Sep 27, 2013 at 22:27
  • [list-item:mail] but you should be looping through all the fetched results before,you can try importing this rule so to get maximum out of it
    – arpitr
    Commented Sep 27, 2013 at 22:41
  • I just need to take this one step further now. Any thought on how to check to ensure the users has the role of "member" before sending the email? Users can flag the content when they are members, but once they cease to be a member, I need to ensure they stop receiving that email. The user account remains but they just don't have the role anymore. Commented Sep 29, 2013 at 4:50
  • This settings seems like for Drupal 7. Any suggestions for 8? I cannot setup it correctly. Commented May 12, 2017 at 10:46
-1

You can use a loop to get all the users who have flagged, and you can add an action to send mail to all those users under that loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.