How to send email notifications for comments in Drupal 7 with actions & triggers (or the Rules module)

Rules module logo Do you have a Drupal 7 web site? Do you want to get an email when someone comments, so you don't have to check your site all the time? Don't panic! Here's how you do it.

You don't need to install any new modules to do this in Drupal 7. All that's needed is a little configuring and to copy and paste in a short bit of code.

Update: Both the easiest & most powerful way to get these notification emails (in Drupal 6 or 7) is to install and configure the Rules Module. NodeOne has a great set of video tutorials on their site explaining how to use the Rules module. I've also put example code for a Rules-based email notification in the comments to this post for both Drupal 6 and Drupal 7

 

Step 1: Getting started

Go to the Modules page, and make sure that you enable the Trigger module (at Modules > Trigger) and turn on Clean URLs (at Administer > Configuration > Search and metadata). These are core modules/settings in Drupal 7, so you don't have to install them, just turn them on.

enabled checkbox for Trigger on the Modules pagelocation of Clean URLs in administrative interface

Step 2: Taking action

Your next step will be to create an action, so that drupal can perform it when a comment is saved.

Go to Configuration > System > Actions.

Actions link shown on Configuration page

On the Actions page, choose the dropdown for Create an Advanced Action at the bottom of the page, and click on Send email and then Create.

create an advanced action dropdown with Send email highlighted

On the Configure an advanced action page, fill in the first 3 fields, as in these examples:

  • Label: Send an email notification to site owner upon comment save
  • Recipient: siteowner@adellefrank.com
  • Subject: New Comment at AdelleFrank.com

The trickiest part of this entire endeavor is filling in the Message field, but here's some easy sample code that you could just copy and paste in:
### Node
[comment:node:title] at: [site:url]node/[comment:node:nid]#comment-[comment:cid]

### Comment
[comment:author:name]
[comment:title]
[comment:body]

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

Those bits of code within square brackets are called tokens. Tokens are placeholders, little bits of code that represent commonly-used values. To find more tokens in Drupal 7, you must have two modules enabled. First, turn on the core Help module, which you should have enabled anyway...because it's helpful.  However, I find it bizarre that, even though you're only using tokens included in the Core, you must still install and turn on the Token module, too.

Only then can you point your web browser to the Help > Token page at http://www.yoursite.com/admin/help/token and get a list of all the tokens you can choose among.

Step 3: Triggering your action

At the top of the page, click on the Triggers link to go to the next step. This page can also be found underneath Structure > Triggers.

Triggers link shown at top of Actions page

Be sure to choose the comments tab on the Triggers page.

Comments tab shown at top of Triggers page

You have a number of choices, but since mine is a solo blog, Trigger: After saving a new comment seems most appropriate. Click on the dropdown instructing you to Choose an action and click underneath system on the name of the Action (Send an email notification to site owner upon comment save) you created in Step 2 and then click Assign.

action selected on dropdown for Trigger: After saving a new comment

successfully assigned action to trigger

Step 4: Testing your comments form

Now, as far as you know, your web site is set up to email you when someone saves a comment.  To be absolutely certain that your new action and trigger are working correctly, you need to test it.

Browse to any page, article, or blog post on your site and post a comment.

Check your email at the address you chose in step 2. Does this email give you the information you expected?

Remember to delete your test comment off of your site.

For more information

Comments

Ed

Token problem

I've made an action which sends an email to the administrator. The trigger is 'when a new user account signed in'.
In that email are tokens like [user:name] and [user:mail], they work very well. I use also tokens such as [current-user:field_address], they are added (non-existing) fields and they don't work.
Does anybody has any idea how I get this working?
Ed

Hans van den Berk

This works!

Thanx a lot for this tutorial. It was all I needed to complete the guestbook.
Also took a look at html email format but that looks like a nightmare so plain text will do for the intended purpose.

boftx

Combine this with user roles

Most of us have noticed by now that spammers are using human comment factories to get past captcha rules.
What I have found to be effective is to have a role called 'Reviewed User' and only allow this role to post comments or content without moderation.
In the case of content, I allow authenticated users to post, but the triggered rule will automatically unpublish any new content unless the user has the 'Reviewed User' role, as well as send an email to me.
I also have a rule that automatically applies the 'Reviewed User' role to the content/comment author when I publish the item. If I don't publish it, I block the user, delete all of that user's content, and report them as a spammer using spambot.
This approach has greatly reduced the headache I was starting to get.

Visitor

Very good!

got me running fast!
Thanks!

Guillermo

Useful

Thanks for the article. It's very interesting this option to manage comments on drupal sites.

Yoni Steingiesser

THANK YOU

Thank you! This was exactly what I needed. You are a life saver. Thank you for putting this up!

barbarae

Best Drupal "How To" I've

Best Drupal "How To" I've read yet - Woohoo - thanks

Calvina

Great!

Thank you Adelle, really clean explanation and working fine in Drupal 7!

vijay

need help

i need to add the comment form on my drupal 7 in an attractive way help me i have just simple form for the comments

Adelle Frank

Sample Email notification code if using Rules in Drupal 7

Here is some sample code you might use in your tokenized email notification.

For the SUBJECT

[comment:hostname] ipsays: [comment:body]

For the MESSAGE

### Block [comment:hostname] IP at
[site-url]user/login?destination=admin/user/rules/add?mask=[comment:hostname] 
[site-url]admin/user/rules/add?mask=[comment:hostname]

### Approve this comment at 
[site-url]user/login?destination=admin/content/comment/approval
[site-url]admin/content/comment/approval

### Comment Title: 
"[comment:title]"

### Blog Post 
"[comment:node:title]"

### Comment Body 
[comment:body]

### Comment Author 
Email: [comment:mail]
URL: [comment:homepage]

Some Important Notes about this Code:

  • The mask in the Block IP address link doesn't work, it's just there for your reference, to make it easy to copy into the correct box on the add mask page.
  • I'm combining notices for both all comments into just one email notification, you might want to separate out Suspected SPAM comments from Probably Safe comments. For more details on SPAM and how you might use Rules to filter, see the comment below at: http://adellefrank.com/blog/how-to-email-notifications-comments-drupal7#comment-1413

stuti

Email to multiple users

Hello, 
I am working in Drupal 7. I need to send an email to all the users of the site and for that I have used rules and trigger module.
But when I am adding a new action : "send message/email", there is only 1 field for the recipient. I need to multiple recipient so that whenever a new content is added in the site, email goes to all the users of the site.
Please help me on this.

Sean

Thanks!

I appreciate the write up!  Thanks!

Joe

Thank You!

Thanks so much for posting this tutorial. I tried using Rules, but this method was so much easier to implement.

Juicy Couture

Very helpful, Adelle! Many

Very helpful, Adelle! Many thanks for looking into this for me. (and thank you for including the relevant links to the specific modules needed to make this work!)

Ade

Now, I wish that I had found

Now, I wish that I had found your very well written article two weeks ago, before I went through figuring this out for myself. It would have saved me some trial, error, and head-scratching, which seems to be very common since I started using Drupal!

Adelle Frank

Glad you found this...

 I, too, wish it had been sooner :)

Visitor

Send to all users

Thanks for this tutorial. My question is can it be used to send an email to all users, not just the site owner.
Or, how can one send an email to all users when something new is posted, article, blog, comment?
Many thanks
Michael

Visitor

Send to all users of a role

Again, thanks for this. It works great, I am at least now able to be notified when there is a new post so I don't have to keep checking. For now, I simply send an email to all users (outside of Drupal). So, the users get notified by me when there is a new post. But, my goal is for this to all happen automatically within Drupal.
I am using Drupal 7 and my hosting server is using php 5.2.9, so the Messaging Framework is not available.
I am wondering if I can send an email to everyone in a certain role, e.g. 'Authenticated User', which would satisfy my need.
If not, I will take your suggestion and investigate using the Rules Module
Cheers,
Michael

Adelle Frank

Rules module

To send it to all users, you would probably need to use the Rules Module. NodeOne has a great set of Rules tutorials on their site that you might find helpful.

Adelle Frank

Sample Email notification code if using Rules in Drupal 6

Here is some sample code you might use in your tokenized email notification.

For the SUBJECT:

[comment:comment-hostname] ipsays: [comment:comment-body]

For the MESSAGE:

### Block [comment:comment-hostname] IP at:
[:global:site-url]user/login?destination=admin/user/rules/add?mask=[comment:comment-hostname]
[:global:site-url]admin/user/rules/add?mask=[comment:comment-hostname]

### Approve this comment at:
[:global:site-url]user/login?destination=admin/content/comment/approval
[:global:site-url]admin/content/comment/approval

### Comment Title:
"[comment:comment-title]"

### Blog Post:
"[node:title]"

### Comment Body:
[comment:comment-body]

 

Some Important Notes about this Code:

  • The mask in the Block IP address link doesn't work, it's just there for your reference, to make it easy to copy into the correct box on the add mask page.
  • In order for the IP address token ([comment:comment-hostname]) to be available for your use, you will probably have to apply this patch (http://drupal.org/node/847566#comment-3174456) to the token_comment.inc file in the token module.
  • I'm combining notices for both all comments into just one email notification, you might want to separate out Suspected SPAM comments from Probably Safe comments. For more details on SPAM and how you might use Rules to filter, see the comment below at: http://adellefrank.com/blog/how-to-email-notifications-comments-drupal7#comment-1413

Visitor

Rules Module

Rules Module worked great. Exactly what I wanted to do.
The tutorial on NodeOne is excellent.
Thanks for pointing me in the right direction.
Michael

Adelle Frank

Textual comparison in Rules might help with SPAM checking

If you install the Rules module, there's a much more flexible way than that listed above to do email notification, you just browse to:

http://yoursite.com/admin/rules/trigger/list

and Add A Rule.

I was especially happy about the ability to check fields (such as the title or body of a comment) for spam keywords, using the "Textual Comparison" condition.

For example, you might compare the content of your comment's body to some spam words, by entering the following into the text comparison fields:

[comment:comment-body]

and this regular expression of spam keywords in the second text field (making sure to check the "Evaluate the second text as a regular expression." box)

[cheap|essays|thesis]

Thanks to http://drupal.org/node/880924 for pointing me in the right direction. 

Anthony

IP Address is Vital

An absolute must to add into the data what to send, is the IP address of the user who posted the comment, so it would be easy to blacklist spammers. The token to display it is: [comment:hostname]

Adelle Frank

Found IP token for Drupal 6!

There is a great patch (only 2 lines more of code) that can be added to the token_comment.inc file and you'll be able to use a token in your email to let you know the IP address of your spammer:

http://drupal.org/node/847566#comment-3174456

Adelle Frank

IP token a great idea

If I ever re-visit this post, I'll definitely add your brilliant idea into the standard set of information to return.

Although, I'll have to change my site to run Drupal 7, first, as I don't think version 6 of the Token module has the hostname/IP address as a token.

Thanks for sharing your brilliance!

Alexander

Thanks!

Thanks for this detailled description about the comments mails. I use self Drupal 7 and need it!

The Light Scriber

Muchas!

Your instructions are clear, direct and they (along with the modules) work fabulously well! Thank you for this great implementation documentation!

Visitor

Found it Thanks - Rules

Found it Thanks - Rules Module will do that

Visitor

Ignoring Trigger for Site Administrator

 
Adelle,
 
I'm using D7 together with actions and triggers on around a dozen sites.  A lot of site members can add new content, and I get sent an email whenever there is new content so I can approve it for publication.  Once published, editors can edit their own content, and whenever they do I again get sent an email so I can make sure everything is appropriate.
My problem is, I'm also creating new content or editing existing content every day, but I still get sent an email even for my own additions or edits.
I don't want to turn the facility off every time I'm on a site.  Is there any way (or any module) that will allow the action and trigger to work correctly when others create or edit content, but which will not trigger an email if it's the site administrator that is creating or editing the content?
 

pike

[comment:body] messed up

Great tutorial, thanks!
IF you use a html format for [comment:body], it gets mangled through a html2text convertor, which doesnt look very nice. plain text works fine..
 
*-pike

Els

Great post

Txs for this post. Very clear, easy and helpful. Great!!

Cek Magdurlari

Awesome find

I hadn't spotted those and they look promising. Looks like they are still having issues with the Tokens though. It would be good for them to have it sorted, I may see if I can help in anyway.
As it stands, I have a comprehensive email notification system using triggers and rules for every type of content on my company site and this works fine, but I am looking forward to taking it to the next stage with better use of roles and token emails. Rules is a genius invention for Drupal and goes along the same line of flexibility and handling you come to expect in modules now.

Chandan Tyagi

Very helpful Tutorial

Thanks Adelle for this awesome tutorial.
This is really helpful and save my time........

David

Awesome, thank you

It took me ages to find simple and helpful instructions on how to do this, thank you so much.

Andreas

Great

This is just to thank you for this great description! Well done!

Mike

problem with link in notification email

I was reading this post and I did what it said. I did get the email sent to me, but the linking within the email doesn't link up to the posting that I placed on the site. This is the message that I get:

A comment was added on the following posting...
The Garland Company, Inc. at:
http://garland.radiuscleveland.com/node/1#comment-

The comment was from...
Anonymous
test6
test6

Please review it and approve, delete or edit

that link goes to a 404 page, not good... This is the site I'm working on - http://garland.radiuscleveland.com/ and it's drupal 7, just downloaded it this morning and have been working on it ever since. What can I do to make those links in the message link up to the actual post or node as you might want to call it? Please let me know if you have a solution, thank you in advance. This is the code in the action that I have

A comment was added on the following posting...
[comment:node:title] at: [site:url]node/[comment:node:nid]#comment-[comment:cid]

The comment was from...
[comment:author:name]
[comment:title]
[comment:body]

Please review it and approve, delete or edit

SteveW

I'm getting the same thing

Just found this page, and thank you very much for the clear explanation. I'm seeing the same thing with the [comment:cid] tag getting left off. Checked for whitespace, that isn't it. Everything else is working perfectly.

Adelle Frank

Let me know if you find the answer

I've fiddled and just can't figure out why the [comment:cid] tag is getting left off for y'all.  If you do figure it out, could you let me know - so I can update my post to address this issue?

pike

comment:url

I have a token called [comment:url] that does the job ..

Adelle Frank

Another idea: Clean URLs

I've tried this out and I can't figure out WHY the [comment:cid] token isn't outputting the anchor tag for the latest comment (it's working fine in my test system).

As for why you don't just get redirected to the page the comment is supposed to be on, I have an idea. I notice on your site you don't have clean URLs turned on, which makes the path for your pages use a ?q= before the node reference in the URL (http://garland.radiuscleveland.com/?q=node/4). Perhaps this is part of the issue?

In fact, I'm going to add this tip to my getting started section.  Thanks, Mike!

Adelle Frank

Check whitespace and permissions

Hey, Mike: 

I have 2 ideas for troubleshooting this issue:

  1. Try removing any whitespace (if there is any) between #comment- and [comment:cid].
  2. Check the permissions and logged-in status of the person to whom this email notification is sent.

Let me know if this helps,

Adelle

Michael

Nice tutorial

Nice tutorial, got me running fast!

Many thanks!

Adelle Frank

You're welcome

You're welcome, Michael.

Thanks for the positive feedback!

Visitor

Works a treat

Thanks - you're a bloody legend!

Did what was needed and more.

Adelle Frank

Glad to help

Thanks, Visitor, I'm glad this helped you!

ztank

Easy way to get notifications on posts where I leave comments?

Hi,

nice post but I'd like to know if there is an easy way to get notified every time a new comment is added to a post where I previously left my comment.

Something like comment_notify module: http://drupal.org/project/comment_notify

AFAIK this one is still in alpha and apparentely there is no other way to implement such a behaviour.

Are you aware an alternative way?

Ciao,
Claudio

Adelle Frank

Comment_notify module looks best

Sorry, Claudio:

I am not aware of an alternate way. :(

I think you're right that the comment_notify module is probably the best solution right now.  Of course, I haven't yet dived into the Rules module enough to know if there's a way that Rules could make this to happen.


Ciao!

Robin

18 months on ...

Adelle, I'm trying something like this using Flag and Rules. I'm trying to use Flag to allow users to "watch" either Articles or Posts or Comments and am trying to get Rules to send an email to users who've set flags. But I'm struggling. Flag looks ok (the buttons are there and look fine), but there're no actions associated with them - no email gets sent... probably because I'm struggling to find how to get Rules to set that up! :D So far I've installed Flag and Rules and then: Token, Entity, VBO, Views and now Ctools. I'm drowning in modules! Can you shed light on the fundamental process here?? Is VBO what I need to send batch emails to users who've flagged things??