Setting up email subscriptions and notifications in Drupal 7

Drupal Level: Beginner-Intermediate

Email notifications are the glitter of community sites: very few people passionately like them, but you'll never get rid of them. Probably the most typical use case on a Drupal site is to send an email notification to let a node's author know that someone's commented on their node. I just spent the good part of an afternoon setting this up on a Drupal 7 site, and thought I'd pass along some time saving tips.

First, a quick summary of where things stand today. There are 3 module I looked at when I was trying to implement this feature, and at first glance, they seem like the right solution, but it turns out that none of them are ready for prime time as far as Drupal 7 goes:

Notify - abandoned, no stable D7 release
Notifications - Seeking maintainer, no stable D7 release (alpha), depends on Messaging Framework, which hasn't been worked on in a year
Subscriptions - somewhat active, has a D7 beta

Ok, if these modules don't work, how do I implement this feature? I went back to the old Drupal 6 standby: Flag + Rules. The D7 versions of these modules make email subscriptions a snap.

First, a note: your site is probably different from mine. This guide is not meant to be a step-by-step how-to, but rather a guide to point you down the right path. I'll be demonstrating this implementation with the following setup in mind:

  • Content type "Question" is my "parent" content type. It's the type where the subscriptions are flagged.
  • Content type "Answer" has a node reference to its parent Question.
  • My use case: I want subscribers to be alerted when one of their subscribed Questions gets a new Answer, and I want to automatically subscribe Question authors to their own Question nodes.

First, install the Rules module.
Then, update Flag module to at least 7.x-2.0-beta6. That's for integration with Rules, which happened in this ticket.

Next, head over to admin/structure/flags/add and create a flag that will be your "Subscribed" indicator. I called mine "watch_this_notify". Fill out the form, with values for "Flag link text" and so forth:

Don't forget to enable Flag Access for the appropriate roles (I chose "authenticated user" only), and set your Flaggable Content (I chose "Question"). Set your link display options, too — I chose to show the link only on the full node.

Next, you have to set up two Rules: one to automatically subscribe Question authors, and one to actually send the notification email.

First, go to the Add a Rule page at admin/config/workflow/rules/reaction/add. I called this first Rule "Subscribe author to own question". The reaction event is "After saving new content":

Add a condition to check for the content type you're subscribing users to (remember, mine is "Question"), then add an Action to add the flag you created above to the node on behalf of [node:author]:

Now, add another Rule to send the email when someone posts an Answer. The Actions on this rule are really cool: first you fetch the users who have flagged the node (in my case, the nodereference'd Question node), then in a loop Send Mail to each of those people (note: you'll configure the content of the email inside that rule). When you're done it'll look something like this:

Now all you have to do is author a node with one user (to verify that the automatic flagging works), then add a response to that node with a second user (to verify the email goes out).