There are 2 ways of customizing the emails that you can get webform to send when a user submits a form:

  • use the built-in email templating interface
  • use a webform email template in your theme directory

Using the built-in email templating interface

This is the easiest way of customising the emails that webform sends.

  1. Go to the Webform --> E-mails screen for the webform in question.
  2. Either use the Edit link for an existing email recipient or Add a new recipient. This should get you to the Edit e-mail settings screen.
  3. Scroll down to the E-mail template settings. Change the Default template to a Custom template. Modify the composition of the email, editing the text as desired. Use any of the Tokens listed under "Token values" to add in the variable data you might need. %email_values is the simplest way of getting all the values entered by the user into the email. You can easily include or exclude the value of a specific component by checking or unchecking the relevant component under "Included e-mail values".
  4. Click Save e-mail settings at the bottom of the page and that's basically it.

Using a webform email template

For greater control over the content of the email, you can use a template in your site's theme directory.

  1. Copy the default webform-mail.tpl.php template (usually found in sites/all/modules/webform/templates/) to your site's theme directory. If you prefer to test the modified webform template from the admin interface, be sure to add it to your site's admin theme directory as well.
  2. Clear the theme registry (e.g. by clearing caches on the Settings/Performance screen, or using devel module) so that the new template is picked up (you need to do this every time you add or remove a template, but not when you modify an existing template).
  3. Edit the template to suit your needs. There are instructions and suggestions in THEMING.txt and in the supplied webform-mail.tpl.php. Note that that putting print_r($submission); in the template file is a very handy way of finding out some of the available variables. Note: if you copied the template to your admin theme as well, then be sure to include the changes in that template file as well.
  4. Note that the output from webform-mail.tpl.php is subject to %-token substitution. If your template produces output from user-submitted data in $submission, it may be possible for users to inject %-token strings into form fields that might reveal data you would rather keep hidden (e.g. %value[some_form_field]). To avoid this, include the %email_values token in your template, and put your email message theme code in webform-submission.tpl.php instead like this:
     if (isset($email)) {
      // Code to generate email message
      ...
    } else {
      // behave like default webform-submission.tpl.php
      print drupal_render($renderable);
     } 
  5. You can have a global template for all webforms, and/or have different templates for particular webforms, as described in the instructions.
  6. In order to use the template you have created, you must set the webform's E-mail template setting to Default on the Edit e-mail settings screen.

Technical note about using email template files: when you select the Default template on the Edit e-mail settings screen, the text area underneath shows the currently active webform mail template (either the default supplied with webform, or one from your theme folder, or the relevant webform-mail-[nid].tpl.php from your theme folder) after PHP processing but before token substitution. Any changes you make to the template file will show up immediately when you refresh the Edit e-mail settings screen, assuming that your changes modify the result of running your template through PHP. (This isn't always the case - for example, you won't be able to see the result of code that only runs for Anonymous users.)

Comments

IamOnStage’s picture

I am currently using Webforms as a block for a specific Content Type.

What i am trying to achieve is to print the Content Type Title either in the form itself or the email template.

Any ideas or directions would be greatly appreciated?

swfindlay’s picture

I have exactly the same issue - did you find an answer to this? (I've searched but not found anything...)

ebren’s picture

I believe %post[entity_type] will do the trick, or at least give you the machine name which you can alter.

to see all the post available data:

print_r($_POST);

in your webform template file

rjperry’s picture

How do I get this :

Submitted values are:
[submission:values]

To out put each value on a separate line in stead of all jumbled together?

Jochen Wendebaum’s picture

For those struggling with the customization of the emails, I provide an example snippet of my template file.

I copied webform-mail.tpl.php to my template folder, renamed it to webform-mail-135.tpl.php as the webform is node/135.

Then I changed the contents to provide the values I need, for example I have a "customer number" text-field with id 54. To print the entered value, I use

print ('<b>Customer Number:</b> '.$submission->data[54][0]);

My site uses profile2 with a profile with machine name "user_main". That profile contains an address. This address is available as a token, so I include it into my email with

print ('<b>Address: </b>[submission:user:profile-user-main:field_postal_address]');

One field (id 51) in my webform is a comment (text area), which I include in the mail only if a value was entered:

if ( !empty($submission->data[51][0])) {
 print ('<br><h3>Comments:</h3>'.$submission->data[51][0]); 
}

Finally the most advanced function is a table of all entries of number fields, but only if the value was changed from the default zero.
I check if the type of the field is "number" with $node->webform['components'][$key]['type'] == 'number'.
In the left column, I provide the machine name of the form field with $node->webform['components'][$key]['form_key'].

foreach ($node->webform['components'] as $key => $component) {
   if (isset($submission->data[$key][0])) {
    if ($node->webform['components'][$key]['type'] == 'number') {
      if ($submission->data[$key][0] > 0) {  
        print '<tr><td>'.$node->webform['components'][$key]['form_key'].'<td>' . ($submission->data[$key][0]);  
      } 
    }
  }
}

Good look in customizing your mail template!

penone’s picture

Thanks for the example. How were you able to get the field id number? (ex. $submission->data[51][0]).

kevster’s picture

Hi Jochen - many thanks for your comments on this, very helpful for me in being able to properly control the email output of my webform! I think worth noting that for these templates to work you need to go to the emails tab of the form > edit then go to the email template and set the default template.

@penone - you can get the id of each form field by going to the form components tab and hovering over the edit link for the field to see the ID number? Or I think by using print_r($submission); you can see each field ID...

Drupal development | SEO optimised build

Anonymous’s picture

Thanks Jochen Wendebaum I am adding Classes to email template file but CSS is no Applying on them.
Any directions would be greatly appreciated

oldenboom’s picture

It did took me quite a while and a lot of testing to find out where to store the copy of the webform-mail.tpl.php template. It just needs to be added in to the actual (child) template, just as the documentation above explained me.

However, when trying my modified form template while being loggedin as admin it does not work. I use the Seven theme as admin theme. I had to add the webmail template to the Seven theme as well. I didn't like that, so I added a Seven child theme, activated that as my admin theme, included the webmail template here and presto, it works.

Included those remarks in the documentation above.

The challenge now is for me to remember to update both templates when making changes.

kmccarthy’s picture

You might try using a symlink in your admin sub-theme to the modified template file in the front end theme to keep changes in one place. If that seems to work for all, then we can update the docs.

arnost’s picture

Hi I want to add the company logo in mail template in signature part, how can I add it ?

Pia Klein’s picture

I just managed to customize a form with preview and email by using markup components and conditionals.

  • Create a markup component.
  • Set it to show up in form and or preview
  • Set conditions for the component under webform conditionals
  • Manage the components in the email that will be sent. It works with both: components set automatically and components included via token

Great!

borwickja’s picture

Could someone explain No. 4 under using a webform email template? It's not clear to me from the example and explanation
1. What the attack vector is
2. If indeed there is an attack vector the proposed solution

For instance the explanation states: To avoid this, include the %email_values token in your template, and put your email message theme code in webform-submission.tpl.php instead like this...

Did whoever wrote this intend to state "put your email message theme code in" webform-mail.tpl.php instead like this...

the comment in the actual code example

// behave like default webform-submission.tpl.php

suggests that this is the case. So if I'm understanding correctly the actual directions are to add an if else statement to webform-mail.tpl.php that checks for the presence of the email variable and if none is provided to render the form. Is that accurate?

japo32’s picture

I'm in the same boat. The instructions are not clear. And given that this can be a security issue, should be explained in better detail.

coxy0001’s picture

What code would I use within the template to put a button on the email that takes the recipient to the website to provide a reply to the original sender?