Sending Comment Posted Emails to Drupal Blog Authors
Thomas Ingham | 05-12-09
How do you go about sending emails out to Blog post authors on a per-post basis using Drupal?
The answer is pretty simple if you know where to look. Scouring around the net you’ll find a lot of advice but let’s have this post be your one-stop shop to setting it up in Drupal 6x. Here’s what you’ll need to get started:
- Drupal 6 Installation
- Triggers Enabled
- Tokens Module
- Actions Enabled
The respective module pages have pretty self-explanatory setup procedures and if you need any further help just hit me up on IRC (irc://irc.freenode.net/#drupal-support) or via the Contact form to the right.
Step 1. Laying the foundation.
Set up your Action by browsing to the Administer>Site Configuration>Actions tab. This whole process can be a bit confusing because the “Add New” stuff feels like it should work like Blocks does, but it must be older functionality; it’s inverted. Scroll down to the select box and select “Send e-mail.”
Because you’ve got tokens enabled, you can use the handy bracket-syntax in fields all over Drupal including on the Actions forms. In this case we’re going to use the tag [author_mail] which comes in from the Node object. This tag isn’t immediately available to the segregated Triggers (supply side) of tokens but we’re going to fix that in a later step.
Enter [author_mail] in the recipient field and fill out the other fields to suit your desired Drupal aesthetic.
Step 2. Get Things Moving
Next up you need to tell Drupal that something needs to be done, when a certain event occurs. This is done using a Trigger. Browse to Administer>Site Building>Triggers and then select the “Comments” object-type tab at the top of the form. This will show you a list of the available events that you can fire Actions off of. For this we want to say that when a “Comment is Saved” we’re going to initiate the Action we created in Step 1. Click the submit button to finalize.
Step 3. We’re Almost There
If you were to flip around to the front of your website right now, select a Blog post and submit a comment you’d get an error that looks like:
“Unable to send e-mail. Please Contact the site administrator if the problem persists.”
Then looking at the error logs you’d see something akin to:
“Error sending e-mail (from user@domain.com to [author-email]).”
The reason for this is because Tokens isn’t providing “Node” information to Comment objects inside of Triggers. This is easily remedied however with a really quick snippet of php code. If you’ve already got a custom module set up for your site; you can skip ahead to the code snippet.
Step 4. Customizing the Crap out of It
In order to get some code into your site to override the default behavior supplied by Drupal you’ll want to create a custom module. The reasons for this are many, but primarily it’s because if an update for Drupal or a module you’re using comes out, you will want to be able to install it without replacing your customizations (thereby breaking your site.)
To create a new module, just make a new directory inside of “[yourproject]/sites/all/Modules/[custom module name]” and then create two files inside of that directory.
1. [custommodule].info
This file stores metadata about your module. The relevant bits of info that you should put here should look like:
; $Id$ name = [custom module name] description = Overrides hook_token_values core = 6.x
2. [custommodule].module
This is the actual source code for the override:
<?php
function [custommodule]_token_values($type, $object = NULL, $options = array()) {
$values = comment_token_values($type, $object, $options);
switch($type)
{
case "comment":
$comment = (object)$object;
$node = node_load($comment>nid);
$account_mail = db_result(db_query("SELECT mail FROM {users} WHERE uid = %d", $node>uid));
$values['nid'] = $node>nid;
$values['type'] = $node>type;
$values['type-name'] = node_get_types('name', $node>type);
$values['language'] = filter_xss_admin($node>language);
$values['title'] = check_plain($node>title);
$values['title-raw'] = $node>title;
$values['author-uid'] = $node>uid;
$values['author-name'] = check_plain($node>name);
$values['author-name-raw'] = $node>name;
$values['author-mail'] = check_plain($account_mail);
$values['author-mail-raw'] = $account_mail;
break;
}
return $values;
}
This selects the “comments” case of the token values stack, and injects some information about the node inline. The handy bit we’re looking for is just author_mail but we’re including the other keys in case we want to make emails feel a bit more personal.
Add this directory to your development site (because we all keep two copies of our site up right, one for development and the other the live environment?) And browse to Administration>Site Building>Modules>Other and enable the module you just added.
Once you’ve done that, you should find that re-submitting a comment on a Blog post appropriately sends an email to the original Blog post author. Score 1 for Drupal. Hit me up on Twitter if you have any questions via @coalmarch

Comments