If you need to send out an email notification in Drupal, the two most common ways are either directly from code, or by using the Rules module.
When I say email, I’m not talking about newsletters, I’m talking about emails that get sent on specific events. For the sake of simplicity, let’s say you send emails using the Rules module.
Previewing and testing emails that are sent using Rules can be difficult. On a decent size project, you’ll end up with a lot of rules and often forget which ones send out emails, unless of course you document your rules well.
However, even if you document your rules, testing and previewing the email is still tough. For example, if you need to change the email body text, there’s no easy way to preview the email without firing off the rule.
Enter, Previewable Email Templates.
The Previewable Email Templates module or PET for short, allows you to create email templates and preview them within Drupal.
Email templates can be sent in three ways:
- Using the module’s preview/send page
- Sent programmatically
- Or, sent using Rules
The email templates are defined as entities: this allows you to export templates using Features (or by hand), clone existing templates and manage the email templates directly from within Drupal.
No longer are you required to search through lots of rules just to find the body text for an email.
If you need to make a change to an email template, just go to the “Previewable Email Templates” page in the administration section, update the template, preview it and you’re done.
Finally, the module does not handle mail delivery. You can’t enable the module and expect emails to be sent. You still need to setup Postfix or Sendmail, or send emails through a third party SMTP provider.
In this tutorial, we’ll setup the module and learn how to create and preview emails. In part two, we’ll learn how to send emails using Rules and we’ll setup Mime Mail to send HTML emails.
Getting Started
Before we begin, download Previewable Email Templates, Entity API and Token. Then, enable Previewable Email Templates, Entity API, Entity tokens and Token.
If you use Drush, run the following command:
$ drush dl pet entity token
$ drush en pet entity entity_token token
How to Create an Email Template
The first bit of functionality we’ll look at is how to create an email template.
1. In the administration toolbar, go to Structure and click on “Previewable Email Templates”.
2. Click on “Add previewable email template” and enter a title, subject and body.
Once you have constructed the email click on “Save template”.
How to Preview Emails
In the last section we created an email template, now let’s see how we can preview the template.
1. Go to Structure and click on “Previewable Email Templates”.
2. Click on the template label.
In my email body I used the [user:name]
token, which is the username.
For this token to render, we need to pass a uid
GET parameter into the page. PET needs to know which user account to use when previewing. If you preview an email without specifying a user ID, any user token in the email body will not render.
So add ?uid=1
to the end of the URL.
From this:
To this:
You can also pass a node ID as well. Just add nid=23
into the URL.
Once you’ve added a nid
or uid
click on Preview.
On the next page you get the ability to preview what the email will look like before it’s sent. Because, I’m passing uid=1
as a GET parameter my “Hi [user:name],” is converted to “Hi admin,”.
To actually send the email, click on “Send email(s)”.
Finally, the module only supports token substitution for node and user entities. If you want to add support for custom tokens then you’ll have to implement the hook_pet_substitutions_alter
hook.
I really like this module because of how easy it is to preview and test emails. You don’t waste time searching through rules or code just to change the text in an email body.
That’s all for the first part in our two part series on PET. In part two, we’ll look at how to send emails using Rules and how to configure Mime Mail to send emails as HTML.
A key problem with emails is providing emails in the language of the user targeted by the mail. Rules is particularly bad about that. Does PET provide some explicit support? Looks like great stuff.
Unfortunately, I don’t think it supports translatable emails. There isn’t a “language” column in the PET entity table to track which language to use.