Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
Many blogging tools and CMSes can automatically send out a notification message when new comments are added to a post. Drupal does not have a setting or checkbox to do this automatically. Instead, in typical Drupal fashion, it gives us the tools to set it up ourselves and customize it as desired using "actions" and "triggers".
For this recipe, we will be using the trigger module (core), as well as the token module (contrib). Token module is not strictly required for this task, but it makes it possible to send a much more informative notification.
The first step is to download and install the token module as you would any new module (download and unpack in sites/all/modules/contrib/). Many modules depend on token module, so there is a good chance you actually already have it installed.
Next, go to your Admin -> Site building -> Modules page (admin/build/modules) and enable the new modules:
- Token
- Token actions

While you are on the Modules page, you'll also want to enable the core Trigger module:

Now it is time to make our first "action". In Drupal, actions are individual tasks that the system can perform, such as unpublishing a piece of content or sending an email. Once we've defined our action, we will be using a "trigger" to activate it. So navigate over to Admin -> Site configuration -> Actions (admin/settings/actions). You'll see a list of available actions, and at the bottom of it a dropdown with a list of "advanced actions" we can customize and make available. Select "Send tokenized e-mail..." from the list, and click on "create".

On the form that comes up, we'll first need to describe our new action. Fill in a short, but descriptive title such as "Send tokenized comment notification e-mail" and note the title you gave it since we'll use it later to connect this action to a trigger. The next field to fill in is recipient. Put in "[author-mail]". This is a token (provided by the "Token actions" module for use in actions). We'll look more at tokens in a bit, but for now just know that it will be replaced with the e-mail address of the node's author when the time comes. Next fill in a subject for your email. Something like "[[site-name]] New Comment". The [site-name] token will also be replaced, so the email notification subject will be something like "[Your Sitename] New Comment".
Finally we come to the message field. This is where we can really utilize some of the many tokens made available by the "Token actions" module and use them to format an informative and customized message. Below the message field is a collapsed fieldset labeled "Placeholder tokens". Click on it, and it will open to display dozens of tokens available for use in your message. Scroll through this list to get an idea of just what options are available. When you are ready, fill in a message using the tokens. I usually use something like this:
### A new comment has been posted on your [type-name]:
"[title]" at:
[site-url]/node/[comment-nid]#comment-[comment-cid]
### Comment information:
"[comment-title]"
Submitted by [comment-author-name] on [comment-ddd], [comment-mm]/[comment-dd]/[comment-yyyy]
[comment-body-raw]
### Please review the comment approval queue at:
[site-url]/admin/content/comment/approval
When you are satisfied with your message, click on "save". This will return you to the actions list, and your new action should be listed.

Now its time to move on to define a trigger that will fire off our new action! Navigate to Admin -> Site building -> Triggers -> Comments (admin/build/trigger/comment). This page lists comment related triggers that can be assigned actions. The very first trigger is "Trigger: After saving a new comment". In the dropdown box for this trigger, select "System - Send tokenized comment notification e-mail" (or whatever title you gave to your action), and click on the "assign" button.

That is it, you have created a custom action, and assigned it to a trigger! Now, to try it out, navigate to a piece of content that you created, and leave a comment. You should receive an e-mail notification about your comment!
Comments
#1 Update
Submitted by Eric Weik on Sun, 12/13/2009 - 13:48.
I have updated this post with some screenshots for clarification.
#2 for blog poster
Submitted by Visitor (not verified) on Sun, 12/13/2009 - 22:38.
nice writeup. I'd be interested in taking the next step and learning how to do this, but allow notifications be sent to each blogger in a multi-site environment. haven't had much time to research it, but i heard comment notify helps with this.
i also use triggers and actions for when something changes workflow states. really great functionality...long live drupal.
#3 [author-mail] == blog poster
Submitted by Eric Weik on Mon, 12/14/2009 - 09:43.
If I understand your requirement correctly, it is covered by setting the recipient to the [author-mail] token. This causes the notification to be sent to the author of the content being commented on, and should work fine even in a multi-site environment.
#4 Comment Notify
Submitted by Eric Weik on Tue, 12/22/2009 - 23:59.
I originally wrote this tutorial primarily to illustrate how to use actions and triggers. While it certainly provides the functionality to notify content authors about comments on their content, those seeking a more fully featured comment notification solution should also check out the comment notify module. It supports not only notification messages for content authors, but notification messages for comment authors as well:
http://drupal.org/project/comment_notify
#5 How to send html based emails
Submitted by Goli (not verified) on Tue, 12/29/2009 - 01:53.
hi,
This is a great tutorial. But I was wondering how can I extend it to send html emails. I want the comment-author, to link to the profile of comment author, and node title to link to node url.
Is there a way to do so?
Any help would be appreciated.
#6 No easy solution
Submitted by Eric Weik on Tue, 12/29/2009 - 11:26.
Goli - I'm not aware of any simple ways to turn "send tokenized email" messages into HTML messages.
If you are comfortable building modules, you should be able to use hook_mail_alter() to alter the message headers. I've not actually tried this, but something like the following should work:
<?php/**
* Implementation of hook_mail_alter().
*/
function YOURMODULE_mail_alter(&$message) {
if ($message['id'] == 'token_actions_send_email_action') {
$message['headers']['Content-Type'] = " text/html; charset=utf-8";
}
}
?>
If this does not work, its likely because I got $message['id'] wrong. I think it is 'token_actions_send_email_action', but you may need to do some debugging to see what the message id actually is.
It might be overkill, but you could also check out the HTML Mail module which will basically turn all messages sent via drupal_mail() into HTML messages:
http://drupal.org/project/htmlmail
#7 Hi, Eric, Thanks. I will look
Submitted by Goli (not verified) on Tue, 12/29/2009 - 22:47.
Hi,
Eric,
Thanks. I will look at html email module, as I have to anyways send newsletter messages from my site. I am not much comfortable with hook form alter. :(
#8 Very practical code example, thanks!
Submitted by Adelle Frank (not verified) on Fri, 01/29/2010 - 12:50.
Thanks so much for posting this.
I get so tired of having to login for moderation every time I get a comment, when 80% of them are spam. This way, I can just delete the email notification when it's obviously spam and go on about my day.
I had no idea how to use actions and triggers, but this is a really clear and relevant example for a lot of us who are drupal site administrators.
You are a sanity-saver!
#9 HA! I just did this today
Submitted by Chris Free (not verified) on Wed, 02/03/2010 - 15:13.
HA! I just did this today then stumbled on to your post, another good one!
#10 Parallel Tracks
Submitted by Eric Weik on Wed, 02/03/2010 - 16:02.
Thanks Chris. We seem to be on parallel tracks at the moment. ^_^
#11 Roles?
Submitted by Ed (not verified) on Tue, 03/23/2010 - 09:15.
Useful post, however the problem comes if you want to send an email to a role.
I guess you can use Rules to add to the forms but then you loose the features of tokens.
#12 Not yet, but maybe soon...
Submitted by Eric Weik on Tue, 03/23/2010 - 13:17.
Its true Ed, this only works for emailing specific users or the content author, not roles. However, it looks like there may be some other options on the horizon:
http://drupal.org/node/329267
http://drupal.org/project/action_email_role
#13 Awsome find
Submitted by Ed (not verified) on Wed, 03/24/2010 - 04:15.
I hadnt spotted those and they look promosing.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 comprehesive email nofiforcation 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 flexablity and handling you come to exspect in modules now.
It is frustrating that this hadnt been looked at earlier though.
#14 Action for a node type
Submitted by Oscar (not verified) on Fri, 04/09/2010 - 13:18.
Hi, i was trying to do an action for my site, for example if someone create a node with the type post, i want to send a email to my user. Only if the node is post type and not other type. Thanx Oscar
#15 Look into "Comment Notify"
Submitted by Eric Weik on Sun, 05/02/2010 - 07:58.
Hi Oscar,
I don't know of a way to achieve what you are looking for with actions and triggers without some module development (either by using hook_drupal_alter() to change an existing action, or by writing a custom "advanced action").
However, I think you'll find that comment_notify.module does provide the functionality you are looking for:
http://drupal.org/project/comment_notify
-Eric
#16 thank you
Submitted by gireesh (not verified) on Wed, 02/16/2011 - 00:00.
Hi
What a fantastic description...Really great.
I appreciate you giving a such good explanation of what need to do exactly to work with triggers and actions.Really helped me
Thank you very much.
thanks,
Gireesh
#17 Post new comment
Submitted by Visitor (not verified) on Sat, 04/09/2011 - 01:24.
I don't know of a way to achieve what you are looking for with actions and triggers without some module development (either by using hook_drupal_alter() to change an existing action, or by writing a custom "advanced action").
#18 List of Emails
Submitted by Visitor (not verified) on Tue, 08/09/2011 - 17:08.
How about sending the notifications to a list of emails rather than only one recipient
Thanks for any help
#19 E-mail content of a blog update
Submitted by Mark (not verified) on Thu, 08/11/2011 - 13:04.
Hi Eric,
I get the basic idea behind the use of the token, action and trigger, but I am new to Drupal and don't know what the code would look like if I wanted to send the actual content of a Blog Update via e-mail. Any thoughts you have would be appreciated.
Thanks
#20 Thanks for the Help
Submitted by Alysa (not verified) on Mon, 08/15/2011 - 13:09.
Great tutorial and gave me exactly what I needed for a very furious ticket exchange board.
#21 Mail Does not send after adding two actions to one trigger
Submitted by SM (not verified) on Thu, 09/08/2011 - 03:41.
Mail Does not send after adding two actions to one trigger
I created two actions like above (chnaged some tokens of course)
So we could notify the author and admin of new comments.
I first tested with author email and it worked, once we added the other action it no longer works.
I un-assigned cleared cache even deleted and re-created and then assigned like new but still no mails come out anymore.
#22 Wanted to Update my Post
Submitted by SM (not verified) on Thu, 09/08/2011 - 04:20.
All is well with the mails being sent to the author, after hours of re-doing things I found them in Gmails SPAM. Arrgghh. Never thought to look there. Thanks for the super cool tutorial. I also added on for when comments are deleted so we have a record of spammer comments we have removed, Many thanks
#23 We will soon upgrade to
Submitted by Cek Magdurlari (not verified) on Fri, 10/21/2011 - 13:36.
We will soon upgrade to Drupal 7, but in the mean time I am looking for a solution that enables us to customize the trigger module or see if it exists in Drupal 7 already.
We would like to alert users when specific content is published using triggers, via email. That you know of, in Drupal 7 - does the trigger module included in the core allow one to do this?
#24 Michelle
Submitted by Michelle (not verified) on Wed, 10/26/2011 - 13:11.
I think this is great. 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. Thank you for the idea that you share.
#25 James
Submitted by James (not verified) on Thu, 10/27/2011 - 11:01.
I think this is nice idea. While it certainly provides the functionality to notify content authors about comments on their content, those seeking a more fully featured comment notification solution should also check out the comment notify module. Thank you.
#26 hid conversion kit
Submitted by hid conversion kit (not verified) on Wed, 11/02/2011 - 04:10.
I found this article is really useful for me in my job. Thanks for sharing this useful information.
Post new comment