Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Rules for admin notifications, Part 1: New user registrations

Parent Feed: 
New Spammer Registered

Rules is an especially useful Drupal module for all kinds of tasks. One use you might want to put it to is providing admin notifications of certain events on your site, e.g. user registrations and the creation of new comments and content by these “untrusted” users (assuming your use case allows them to create any content at all). I recently created such rules to help monitor the creation of users, content, and comments on drupal.cocomore.com/.de. Since we use the Project module (and supporting code) to host and track issues on some Drupal modules, we allow users to create accounts and “Issue” nodes. But there hasn’t been much recent change to the modules we host, so most of the “users” turn out to be spamming scumbags who post “issues” with links to questionable sites (you know the type). Since we allow anonymous users to comment on our blog posts, we also get our fair share of comment spam, but a tricky Captcha (we’re using Riddler, these days, to filter out visitors who don’t know or can’t take the time to search the answers to simple Drupal trivia questions) helps keep comment spam to a minimum. Keeping vigilant about stomping out spam is important since leaving spam published looks unprofessional and is bad for SEO… and since it also attracts more spam (spammers see that your site leaves spammy links in place); but of course it’s also important to keep an eye on the valid posts, too, and to respond to them in a timely fashion.

So we will assume that you have a site without a massive flow of new user registrations or new content and that you want to be alerted with some useful information whenever these events occur so that you can take appropriate action (block users and clean out the spam… or respond to valid content/comments). This article will lead you, step-by-step, through the creation of three different rules on both Drupal 6 and Drupal 7 -based sites, identifying particular set-up differences between these versions of Drupal/Rules. The three events we want to create Rules for are:

  1. New user registered
  2. New comment posted (by non-staff user or “untrusted” user)
  3. New content posted (again, by some kind of “untrusted” user)
In each case, we simply want to send an HTML email* to notify at least one member of staff (anyone with the admin role and, in the case of comments on blog posts, we want to also email the article’s author.) This article does not get into the various particulars of configuring your server to be able to send mail; there are a number of factors which might differ from server to server and it’s not really within the scope of a Drupal-related article.
*Note: This article also does not cover setting up HTML mail, but some modules, such as Mime Mail help make this a relatively pain-free process and provide a “send HTML mail” action for Rules. Adding specialized modules is probably not justifiable if you don’t plan to use HTML mail for anything more than admin notifications, but if you want to email users, such modules can help you create much more attractive and useful emails.

In every case, creating a new rule starts by going to the “add rule” page:
D6: admin/rules/trigger/add
D7: admin/config/workflow/rules/reaction/add

Notify admin when a new user registers

This is a simple rule which sends an HTML email with a link to a new user’s profile, along with their username. If you allow users to register themselves on your site, you will likely notice patterns that persistent spammers follow and be alert enough to just block the most suspicious user accounts before they even start spamming your site. I won’t specify the suspicious patterns I’ve been reacting to here (I don’t want to teach spammers how to be sneakier or more effective), but if you have a spam problem, you probably already know the patterns or will quickly recognize them.

Adding a new rule to react to user registrations

The first step is virtually the same on both Drupal 6 and Drupal 7. Pick a name for the rule that you will not be confused by later, add tags (categories) if you have lots of rules, or plan to make lots, and select the event, “After saving a new user account” (“User account has been created” on Drupal 6).

Drupal 6 - Rules Event is a “new user account”Drupal 7 - Rules Event is a “new user account”

Click “Add action”

The “Add action” pre-step is also almost identical in Drupal 6 and Drupal 7 and we will skip over illustrating this in the following rules:

Drupal 6 - Rules “Add Action” buttonDrupal 7 - Rules “Add Action” button

Add action “Send mail” or “Send HTML mail”

In Drupal 6, the options for sending an HTML mail (provided you have Mime Mail installed) are separate from the rest of the “Send mail…” options. In Drupal 7 they all appear together in the “System” options. Either way, you can choose to send an email to an appropriate person or group of people. The “arbitrary email address” options can be useful if you want to send email to more than one person, regardless of their site roles. You could, for example, send a mail to the author of content that received a comment (using a “replacement pattern”, i.e. “token”) and/or to a particular person. More than one email address (or token substitute) can be added to recipient fields as long as the addresses are comma-separated.

drupal6-rules-send_html_mail.pngDrupal 7 option - Send HTML mail with Rules

Complete the email subject and body

The email “subject” and “body” fields can accept a variety of tokens or “replacement patterns”. In Drupal 7, these are apparently separate from “Tokens” provided by the Drupal contrib module, Token, which of course you also have installed; the Rules replacement patterns are available even if Token is missing or disabled, but in Drupal 6, you need the Token module enabled in order to have “replacement patterns” available for use.

Drupal 6 - Configure Rules message for new user registrations drupal7-configure-message-new-user-4.png

Download import code for this rule

Rules allows you to export and import rules from saved code, so I have attached an export of this rule. Even if you don’t want exactly the same action, this could be useful as a starting point for a new rule on your site. There are four versions of the import code attached, two each for Drupal 6 and Drupal 7 (for each version of Drupal, there is one version of the rule which uses HTML mail for the admin notification email, and one which just sends a plain mail.)

Troubleshooting

I suggest initially triggering a very simple rule to send a very simple email (to test that you get your message) and then build up the complexity a small step at a time, testing as you go.

AttachmentSize
1 KB
666 bytes
1.99 KB
2.76 KB
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web