How to Send HTML Emails in Drupal 7 using Rules

Whenever I post a tutorial about sending emails using Drupal, the first question I get asked is how to send emails as HTML. By default, emails are sent as plain text in Drupal 7. There are a few modules out there that allow you to send emails as HTML like Mime Mail, HTML Mail and Swift Mailer.

There are a few benefits in sending emails as HTML. For one thing, they can be styled and images can be displayed. Also, it’s easier to track open rates and clicks if the email is HTML. However, there are some downsides to using HTML. One thing I’ve read about is that HTML emails are more likely to be flagged as spam. Especially if your whole email is just a single image.

In this tutorial, you’ll learn how to use Mime Mail to configure Drupal to send HTML emails. We’ll also look at how to send them using Rules.

Getting Started

Like all tutorials, let’s begin by downloading a few modules. Go ahead and download Rules, Entity API, Mime Mail and Mail System.

If you prefer Drush, run the following command:

drush dl rules entity mimemail mailsystem

Once everything has been downloaded, enable Mail System, Mime Mail, Rules and Rules UI.

Step 1: Configure Mail System

So head over to Configuration, “Mail System” and make sure that MimeMailSystem has been selected in the “Site-wide default MailSystemInterface class” and “Mime Mail module class” drop-down list.

Fig 1.0

Step 2: Configure Mime Mail

Now, head over to Configuration and click on “Mime Mail”. From this page you can change a few of the module’s configuration options. The one we’re interested in, for now, is the “E-mail format”.

Fig 1.1

Change it from “Plain text” to “Filter HTML”. This means that when emails are sent they’ll be filtered by the “Filter HTML” text format.

At this point, you should be able to send HTML emails. If certain tags are being filtered out, make sure that the text format selected in “E-mail format” allows the tags.

Send HTML Emails using Rules

Mime Mail integrates beautifully with Rules. The module implements two custom actions: “Send HTML e-mail” and “Send HTML mail to all users of a role”. As you may have guessed, both actions allow you to send emails as HTML.

I won’t go through the process of creating a basic rule. I’ll assume you’ve already created one. So go and edit a rule that will send an email.

Click on “Add action” and select “Send HTML e-mail” or “Send HTML mail to all users of a role” from the drop-down list.

Fig 1.1

As you can see, sending HTML emails from Drupal is not that hard. However, if you’re emails are still being sent as plain text and you have a lot of modules installed start reviewing your existing setup. Occasionally, a module might change some mail setting that prevents an email from being sent as HTML.

FAQs

Q: Emails are not being sent as HTML.

Double check the “E-mail format” option in Mime Mail and make sure it’s not set to “Plain text”.

Q: Emails are not being sent at all.

First make sure you’ve setup a local mail server to send emails. Without a mail server or a 3rd party SMTP provider, emails will not magically be sent. Also, look in the watchdog table for PHP or mail errors.

Q: Rules is not sending emails.

First, make sure your rule is being executed. Once you’ve confirm this, then make sure that you’re using the “Send HTML e-mail” or “Send HTML mail to all users of a role” action.

Q: I’m getting “Unable to send e-mail. Contact the site administrator if the problem persists.”, please help!

This means Drupal got an error trying to send an email. Chances are your mail server is not setup properly.

Q: How do I setup an email server on Windows/Linux/Mac?

An email server can be setup in many ways, you should be able to find a tutorial on the internet.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of WebWash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 12 years and has successfully completed several large Drupal projects in Australia.

18 thoughts on “How to Send HTML Emails in Drupal 7 using Rules”

  1. How to sent entire node page content using this solution including layout and css?

    I use this solution and insert [node:title] [node:filed_image] [node:body]

    all i get is just text of title and body, not the entire node page content.

    Thanks before

    1. I’m not sure about the layout, but you can include the theme’s CSS. Just check “Include site style sheets” from within the “Mime Mail” configuration page.

  2. Thank you for the post, Ivan. My question is, can I use this procedure to send a node as an HTML email?

    Let’s say I have a content type Newsletter, and a role called subscriber.

    Can I have a rule Event: node is saved.Action: send node as HTML email?

    Are you aware of something like this?

  3. I’m just trying out the procedure in your excellent article, and not seeing anything about this field. So I’m wondering what this field is, and what is the best way to use it.
    Thanks for your help.

  4. i see that u really explained how to send a html mail but u did’nt describe how to theme , how to include css style sheets?

  5. Hi,

    Thanks for this great article. I just need to attach some of the images of the current node with the email in Drupal 7. In my application there is a button to share the web page content via the email and I want to implement the above feature.
    Thanks in advance.

  6. Reading your post I have some much more fundamental questions.

    I have set up my VPS LAMP server basically from scratch, and added services one by one as needed.

    It works very well as a production web server, but for the moment I can’t even send a simple email from it, not to speak of HTML emails (I use Zoho as email host for my @mydomain.org emails by redirecting my MX records to their servers and want to relay my outgoing emails through their SMTP server, like we do with Google Apps).

    So my question is, what is your server setup? Do you use some automated service provided by your host? Can some Drupal modules make up for more dedicated services like sendmail, postfix or ssmtp?

    Thanks in advance!

  7. I have two questions.
    1. Using Swiftmail I have html emails sending through SMTP to sendgrid. All is good. emails get delivered. The problem is they show as “threaded” in gmail. Example an email comes in and the text is purple and it shows as a “…” that i click and then it opens up. Any advice about this?

    2. do the site emails that get sent when user creates an account usually show up in the Log Messages? I see the ones activated by a Rule, but not the initial signup emails. The user gets them but just not shown in the logs.

    1. Hi Tim,

      > 1. Using Swiftmail I have html emails sending through SMTP to sendgrid. All is good. emails get delivered. The problem is they show as “threaded” in gmail. Example an email comes in and the text is purple and it shows as a “…” that i click and then it opens up. Any advice about this?

      This happens when Drupal adds too much inline CSS into the email (https://www.drupal.org/node/581328).

      Drupal could be adding the whole style.css from your theme into the message.

      Try reducing the amount of markup and CSS in the message.

      > 2. do the site emails that get sent when user creates an account usually show up in the Log Messages? I see the ones activated by a Rule, but not the initial signup emails. The user gets them but just not shown in the logs.

      I don’t know if Drupal logs the user creation email.

      Cheers,
      Ivan

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Media Management in Drupal

Download a FREE 8 part video course on managing media in Drupal.