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.
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”.
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.
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.
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
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.
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?
I think what you’re trying to do is create a basic newsletter system? If so, then look at Simplenews: https://drupal.org/project/simplenews
Thanks, you are exactly right!
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.
It’s part of Drupal’s mail system and it’s used as a unique identifier for sent emails. Just enter in a string like “contact_form_email”.
Look at the drupal_mail function for more details. (https://api.drupal.org/api/drupal/includes%21mail.inc/function/drupal_mail/7)
That’s brilliant. Your procedure has worked really well.
Thanks so much.
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?
Look at using HTML mail module. https://www.drupal.org/project/htmlmail
It lets you send emails using the theme CSS.
By the way, if you’re using something like GoDaddy for relaying email and hosting with Pantheon this won’t work.
Some service providers will require a PTR/SPF record to be in place in order to accept the email which Pantheon doesn’t support (https://pantheon.io/docs/articles/sites/code/email/). GoDaddy relays are awful anyways.
Gmail is a better bet here.
Thanks for the heads up.
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.
Hi Rakesh,
Watch the following YouTube video:
https://www.youtube.com/watch?v=T-5pu1TSFIg
And look at this link:
http://drupal.stackexchange.com/a/151294/247
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!
Hi,
I normally use a third-party email provider.
Look at:
– Mailgun: http://www.mailgun.com/ (https://www.drupal.org/project/mailgun)
– Send grid: https://sendgrid.com/ (https://www.drupal.org/project/sendgrid_integration)
Both services offer free plans.
Cheers,
Ivan
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.
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
how sent e-mail with HTML link use Rules in drupal 9?
No idea, haven’t used rules in D8 or D9.