In part one, I introduced you to Previewable Email Templates or PET for short. We learnt how to create email templates, preview and send them directly within Drupal.
In this tutorial, we’ll create a rule action that sends an email when a user account has been canceled and deleted. Also, the email will be sent as HTML instead of plain text.
It’s important to note that Drupal can notify users when their account has been canceled. You don’t need to setup Rules or PET to send the email. However, it’s off by default, just head over to the “Accounts settings” page and check the “Notify user when account is canceled.” checkbox to enable the notification.
But as a demonstration, we’ll recreate this functionality using PET and Rules.
It’s important to note that you can not translate email templates using PET into other languages. If you need multilingual support then look at Mail Editor.
If you haven’t read part one, I would highly recommend that you read it before continuing this tutorial.
Before we begin, make sure you’ve read part one and installed all the required modules in that tutorial.
Then, download and install Rules, Mime Mail and Mail System.
If you use Drush, run the following command:
drush dl rules mimemail mailsystem
drush en rules rules_admin mimemail mailsystem
How to Send HTML Emails
The first bit of work we need to do is configure Mail System and Mime Mail so that we can send out HTML emails.
1. Go to Configuration, “Mail System” and make sure MimeMailSystem is selected in the “Site-wide default MailSystemInterface class” and “Mime Mail module class” drop-down list.
2. Now head over to Configuration and “Mime Mail”. Uncheck the “Include site style sheets” checkbox and select “Filtered HTML” from the “E-mail format” drop-down list.
It’s important to take note of the text format selected in the “E-mail format” drop-down list, because certain HTML tags could be filtered out if you’ve configured your text format in a specific way.
For example, I selected the “Filtered HTML” text format and I know that if I add a
<table> tag into the email template it will be filtered out. This is because the text format uses the “Limit allowed HTML tags” filter and the
<table> tag is not listed as an allowed tag.
If something is getting filtered out of the emails, make sure your text formats are configured properly. I also recommend that you create a separate text format just for emails. This way you won’t accidentally change an important setting in a text format that is used across your whole site.
Create Email Template
Now we need to create an email template that will be sent by Rules. If you read part one, then you should already know how to create a template.
1. Go to Structure, “Previewable Email Templates” and click “Add previewable email template”.
2. Call the template “Account canceled” and add
Account details for [user:name] at [site:name] (canceled) into the subject field.
3. Copy the text below into the Body field:
Dear [user:name], Your account on [site:name] has been canceled. -- [site:name] team
Once you’ve filled out the form click on “Save template”.
Let’s quickly preview the email and make sure it’s working.
Click on the “Account canceled” label and add
?uid=1 to the URL after
Remember, for the
[user:name] token to be rendered, PET needs a user ID passed into the page.
Now click on the Preview button.
On the preview page, you should see that
Dear [user:name], has been changed to
Dear admin, or whatever the username is for your user 1.
Now that we have an email template let’s create a rule.
How to Send Email Templates using Rules
In the last section, we created an email template that will be used to notify a user that their account has been canceled and deleted. As mentioned in the beginning of this tutorial, Drupal can notify users when their account has been canceled. But for demonstration purposes, we’ll rebuild this functionality using PET and Rules.
1. Go to Configuration, Rules and click on “Add new rule”.
2. In the Name field enter in “Send account canceled email”, select “After a user account has been deleted” from the “React on event” drop-down list and then click on Save.
3. Click on “Add action” in the Actions area and select “Send PET mail” from the “Select the action to add” drop-down list.
4. Select “Account canceled” from the Value drop-down list in the “The previewable email template to use” field-set.
5. Scroll down the page until you get to the “Recipient (for sending to a user provided by a Rules event)” field-set. Click on the data selector and select
account (deleted user).
PET will send the email to the address attached to the
account selector. If this isn’t setup, then the module won’t be able to send the email.
6. Now in the “User for token substitutions (if any)” field-set click on the data selector and again select
account (deleted user).
Because we have
[user] tokens in the email template; PET needs an account to render the tokens. This is the equivalent of passing the
uid=1 GET parameter on the preview page.
7. Finally, scroll down and click on Save.
Now that everything is setup, let’s test the rule by deleting a user account.
1. Go to the People page in the administration section and edit a test account.
2. Scroll down to the bottom and click on “Cancel account”.
3. Select one of the options that starts with “Delete the account and…”. Remember, our rule will only be fired on the “After a user account has been deleted” event. There is a difference between disable and deleted.
4. Once you’re ready click on “Cancel account”.
If everything is working, an HTML email should have been sent to the canceled user’s email address.
That’s all for our two part series on using Previewable Email Templates. If you’re looking for an easy way to manage and preview emails, then PET is the module for you.
2 thoughts on “An Introduction to Previewable Email Templates Part 2: How to Send HTML Emails using Rules”
The two part series of introduction to PET is really helpful.
Is there any tutorial which shows how to add CC or BCC using PET?
thanks in advance.
It’s been a while since I’ve used this module. After reading this issue, https://www.drupal.org/node/2097881, it looks like the module doesn’t support CC.