Skip to main content
  • Home
  • Work
  • Photography
  • Contact
  • Client Extranet
New Rivers Digital
Home Blogs Eric Weik's blog

Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments

In:
  • Drupal 6
  • Drupal Recipes
  • Token.module
  • Trigger.module
12Dec2009

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!

  • Eric Weik's blog

Comments

#1 Update

Eric Weik's picture

Submitted by Eric Weik on Sun, 12/13/2009 - 13:48.

I have updated this post with some screenshots for clarification.

  • reply

#2 for blog poster

Visitor's picture

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.

  • reply

#3 [author-mail] == blog poster

Eric Weik's picture

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.

  • reply

#4 Comment Notify

Eric Weik's picture

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

  • reply

#5 How to send html based emails

Goli's picture

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.

  • reply

#6 No easy solution

Eric Weik's picture

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

  • reply

#7 Hi, Eric, Thanks. I will look

Goli's picture

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. :(

  • reply

#8 Very practical code example, thanks!

Adelle Frank's picture

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!

  • reply

#9 HA! I just did this today

Chris Free's picture

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!

  • reply

#10 Parallel Tracks

Eric Weik's picture

Submitted by Eric Weik on Wed, 02/03/2010 - 16:02.

Thanks Chris. We seem to be on parallel tracks at the moment. ^_^

  • reply

#11 Roles?

Ed's picture

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.

  • reply

#12 Not yet, but maybe soon...

Eric Weik's picture

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

  • reply

#13 Awsome find

Ed's picture

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.

  • reply

#14 Action for a node type

Oscar's picture

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

  • reply

#15 Look into "Comment Notify"

Eric Weik's picture

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

  • reply

#16 thank you

gireesh's picture

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

  • reply

#17 Post new comment

Visitor's picture

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").

  • reply

#18 List of Emails

Visitor's picture

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

  • reply

#19 E-mail content of a blog update

Mark's picture

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

  • reply

#20 Thanks for the Help

Alysa's picture

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.

  • reply

#21 Mail Does not send after adding two actions to one trigger

SM's picture

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.

  • reply

#22 Wanted to Update my Post

SM's picture

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

  • reply

#23 We will soon upgrade to

Cek Magdurlari's picture

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?

  • reply

#24 Michelle

Michelle's picture

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.

  • reply

#25 James

James's picture

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.

  • reply

#26 hid conversion kit

hid conversion kit's picture

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.

  • reply

Post new comment

Warning
I strongly encourage and welcome links and feedback. However, this site is moderated and comments with inappropriate links are rejected. Please do not post a one-line "Me too" or "Great post!" comment just so you can link to your site. Thank you for your understanding.
The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Use to create page breaks.

More information about formatting options

Blog Posts (RSS)

About

Hello! My name is Eric Weik. I am a computer scientist, photographer, musician, and occasional blogger. New Rivers Digital is my software consulting business. I am dedicated to using open source software and open data standards for Web development and applications integration. In particular, I am an ardent Drupal fan and specialize in Drupal module development, theming, and data architecture integration.

Contact Details

New Rivers Digital
PO Box 784

Lancaster, VA 22503

Voice+1-804-577-8526
Fax +1-804-462-3229
Contact Form

Content Tags

Abstract B&W Celestial Clouds Drupal 6 Drupal Recipes Drupal Sites Estuary Etsy Government Grasses HDR Long Exposure Macro New Rivers Digital Orton Photoblog Photo Expedition Photoset Renderblog RGB Sketchbook Snow Storm Structure Synth Sunflow Sunset Theming Webform.module Wide Angle Zen Zen Theming
more tags

Recent comments

  • This is stunning
    2 weeks 5 days ago
  • #42 worked well for me
    3 weeks 3 days ago
  • Email by country
    12 weeks 3 days ago
  • hid conversion kit
    13 weeks 3 days ago
  • James
    14 weeks 2 days ago
  • Michelle
    14 weeks 3 days ago

Popular content

Today's:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Implementing Flickr Slideshow Links By Theming Flickr.module
  • Non-Unique Conditional Email Addresses with Webform.Module

All time:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Sending Multiple Customized Confirmation Messages with Webform.Module
  • Non-Unique Conditional Email Addresses with Webform.Module

Activity Stream

  • Sun, 01/29/2012 - 22:31

  • Flickr Eric posted #0228 - Sunset Tree 10:31pm #
  • Flickr Eric posted #0225 - Rappahannock 10:30pm #
  • Flickr Eric posted #5617 - Hobie Sailing 11:56am #
  • Mon, 01/16/2012 - 10:43

  • Flickr Eric posted #5687 - Broken Tree (Digital Sketch) 10:43am #
  • Fri, 01/06/2012 - 07:54

  • Flickr Eric posted #8975 - Greenvale Creek 7:54am #
  • Flickr Eric posted #8824 - Grass (blue ch) 7:46am #
  • Flickr Eric posted #5687 - Broken Tree (IR) 7:35am #
  • Flickr Eric posted #3205 - Toadstool 7:21am #
  • Flickr Eric posted #2318 - Tree and Beach 7:13am #
  • Flickr Eric posted #2251 - Low Tide Arrival 7:04am #
more from my activity-stream


I am a member of the Drupal Association.
Eric At NRD on Drupal.org
Circumjacence (Eric Weik) on Twitter
Circumjacence on Delicious
Eric Weik on Linkedin
Circumjacence (Eric Weik) on Flickr
Circumjacence (Eric Weik) at StumbleUpon

Powered by Drupal & Genesis | Valid XHTML 1.0 Strict | Syndicate content RSS Feed

© 2010 New Rivers Digital | PO Box 784 | Lancaster, Virginia 22503 | +1-804-577-8526 | Contact Form