How to send email notifications for comments in Drupal 7 with actions & triggers (or the Rules module)
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
- Step 2: Taking action
- Step 3: Triggering your action
- Step 4: Testing it out
- For more information
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.
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.
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.
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.
Be sure to choose the comments tab on the 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.
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.
Comments
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?