Profile picture for user admin
Daniel Sipos
12 May 2014

Have you ever wondered how you can include HTML markup in the emails you send with Webform? Out of the box, you cannot. But I am going to show you a simple way to achieve this using the Mime Mail module and some simple theming. Additionally, I will show you how to control which webforms should send HTML emails and which should not.

First though, make sure you install and enable the Mime Mail and Mail System modules (the latter is a dependency of the former). With Drush, all you have to do is use this command:

drush en mimemail -y

It will take care of all you need to do. If you commit the module to your repo, don't forget that the Mail System module also gets downloaded, so make sure you include it as well.

Next, edit your theme's template.php file and paste this block of code (explained after):

function your_theme_webform_mail_headers($variables) {
  
  $headers = array();
    
  $headers = array(
    'Content-Type'  => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
    'X-Mailer'      => 'Drupal Webform (PHP/'. phpversion() .')',
  );
  
  return $headers;
}

Make sure you change your_theme with the name of your theme. So what happens here? We override the theme_webform_mail_headers() declared by the Webform module. We do this in order to add a content type to the mail headers, and set it to HTML. And that's pretty much it.

If you now clear your caches and test a webform, you'll see that you can add anchor tags and other basic HTML tags.

One problem you might run into though is that all your webforms are now sending emails in HTML format - a result only partially desired. You'll notice that the default email that you send no longer provides any spacing and all the text gets put on one line - as HTML in fact.

So what you can do is make a selection of webforms for which you'll want HTML emails. A handy way of doing this is by adding a field to your webform content type that will be used to swith HTML emails on/off for a given node. So to illustrate this, let's say we added a new field to the relevant content type called HTML Emails (with the machine name: field_html_email). This field is a boolean type (a single checkbox basically) with the values of 1 for on and 0 for off.

It follows to adapt the theme override above and replace it with something like this:

function your_theme_webform_mail_headers($variables) {
  
  $headers = array(
    'X-Mailer' => 'Drupal Webform (PHP/' . phpversion() . ')',
    );
  
  // Get the HTML Email field
  $html_email_field = field_get_items('node', $variables['node'], 'field_html_email');
  
  // Check if this webform node needs to send HTML emails
  if (!empty($html_email_field)) {
    $html = $html_email_field[0]['value'] == 1 ? TRUE : FALSE;
  }

  if ($html === TRUE) {
    $headers['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
  }
  
  return $headers;
} 

If you consult the documentation for this theme function, you'll know that the $variables parameter contains also the node object which uses Webform to send the email. So we basically check for the value of our field and if it is 1, we add the HTML information to the mail headers. Otherwise, we return the $headers array containing the value it does by default (essentially making no changes).

You can now clear the caches and test it out. Edit a node of the respective content type and check the box. You'll see that it now sends HTML emails. However, if you uncheck the box, it will fallback to the default format that comes with the Webform module.

Hope this helps.

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.

Contact us

Comments

Liran Tal 12 May 2014 21:17

consider revising

In your_theme_webform_mail_headers() hook, it might not be a good idea to specify the php version (phpversion()) from a security perspective, as this is just another way for attackers to fingerprint your web application and gain more insight on attack vectors.

Also, you'd probably want to check if the entire multi-dimentional array is set at:
if (!empty($html_email_field)) {} such as: if (!empty($html_email_field[0]['value'])) {} before you are accessing that possibly uninitialized variable

Daniel Sipos 12 May 2014 22:00

In reply to by Liran Tal (not verified)

Hey there,

Hey there,

The use of phpversion() is default to the Webform module. You can find the definition here (also linked in the article).

field_get_items() will return nothing if the boolean field has no values set at all and will return [0]['value'] if 1 or 0 is set.

D

Alex Morco 06 Aug 2018 10:38

Thanks for sharing this info

Thanks for sharing this info file, I have been creating drupal 8 webform using form module and it helped me while working in the info file. Here's another guide (https://www.cloudways.com/blog/using-drupal-8-webform-module/) that helped me while fixing file issue.

Add new comment