Middlebury

Library & ITS Wiki

Library & ITS Wiki

Drupal Webform Email Templates

The default email template for the Drupal Webform module will send out the date the form was submitted, the name of the person submitting the form (if they were logged in to the site) or their IP address, the value of each field in the form submission and a URL to a page on the site where the form submission can be viewed or edited. This information is usually sufficient, but you might want to have different fields sent to different people when the form is submitted, or provide a custom message to the person who submitted the form.

To do this, you need to use the Custom Template feature which we will describe.

Contents

A Sample Form

For this documentation, we'll use the Web Feedback Form as an example. The form has three fields: a textfield to collect a URL, a textarea to collect comments about the URL and an email field to collect the email address of the person submitting the form. In order to send an email to the person submitting the form, you need to use an email field rather than a text field. The email field will check to see if its value looks like a valid email address and warn the user if it is not.

This is how the form appears to the user:

Webform Feedback View.png

This is how the Form Components screen appears to an editor:

Webform Feedback Components.png

By clicking on the Edit link to the right of one of the form components you can see the Label and the Field Key. The Field Key will be important when we are setting up the email template. It is a good idea to write down the Label and Field Key for each of your form components on a piece of scrap paper for reference as you create the email template.

This is how the Label and Field Key appear on the Form Component editing form:

Webform Form Component Edit.png

Here are the Label and Field Key values for our sample webform.

Label Field Key
Site URL site_url
Comments comments
Your Email your_email

Adding an Email to the Form

For this example, we will add an email to the Web Feedback Form that sends a confirmation message to the person who submitted the form. To do this, first navigate to the form page and then follow these steps:

  1. Click the Edit link below the form.
  2. Click the E-mails link in the Edit Console.
  3. Click the radio button next to Component value and select the field on your form that collects the user's email. In our example this is the "Your Email" field.
  4. Click the Add button.
  5. In the E-mail subject area click the Custom radio button and enter the subject of the email that you would like to send to the user.
  6. In the E-mail from address area click the Custom radio button and enter your email address or the email address for your department.
  7. In the E-mail from name area click the Custom radio button and enter your name or the name of your department.
  8. In the E-mail template area select Custom template from the drop-down menu.
  9. In the E-mail template area enter the HTML text for your custom email. (see below)
  10. Optionally, check the Include files as attachments checkbox.
  11. Click the Save e-mail settings button.

The HTML E-mail

Custom emails are sent using HTML, which is the Hypertext Markup Language. While there are a lot of features of this markup, you don't need to know all of them. Here are examples of some common things you might want to do in your email.

HTML Text
<p>First Paragraph</p><p>Second Paragraph</p>

First Paragraph

Second Paragraph

First Line<br />Second Line after Line Break
First Line
Second Line after Line Break
Regular Text, <b>Bold Text</b>
Regular Text, Bold Text
Regular Text, <i>Text in Italics</i>
Regular Text, Text in Italics
Regular Text, <u>Underlined Text</u>
Regular Text, Underlined Text
<a href="http://www.middlebury.edu">Link to Middlebury</a>
Link to Middlebury
<ol><li>First List Item</li><li>Second List Item</li></ol>
  1. First List Item
  2. Second List Item
<ul><li>First List Item</li><li>Second List Item</li></ul>
  • First List Item
  • Second List Item

Token Values

Tokens can be used to replace part of your email template with information from the form submission. Tokens begin with the percent character (%) followed by some text. Here are the built in tokens and what information they represent.

Token Description
%username The name of the user if logged in. Blank for anonymous users.
%useremail The e-mail address of the user if logged in. Blank for anonymous users.
%ip_address The IP address of the user. An IP address is like the address of a computer on a network. IP addresses beginning with 140.233 are from computers on the Middlebury network.
%site The name of the site (i.e. Middlebury)
%date The current date, formatted according to the site settings.
%nid The node ID of the webform. Useful if you need to create a link back to the form.
%sid The unique submission ID.
%title The title of the webform.
%submission_url The URL for viewing the completed submission.
%email_values All included components in a hierarchical structure.
%email[key] A formatted value and field label. Do not include quotes.
%value[key] A value without additional formatting. Do not include quotes.

We will focus on these last two tokens, since they provide the most flexibility for formatting your custom email template. The key in each is the value of the Field Key that we found when creating our sample webform. Using the %value[key] token, we can create a custom email as follows:

<p>Thanks for your feedback on our site at %value[site_url]!</p>
<p>We received the following comments from you, but we're always happy to hear more.</p>
<p>%email[comments]</p>

Which will appear in the email like this:

Thanks for your feedback on our site at http://www.middlebury.edu!

We received the following comments from you, but we're always happy to hear more.

Comments:
This is a great website!

Note the difference between using %value and %email. With %value only the value of the field appears in the email. With %email both the value of the field and the Label appear in the email.

Fieldsets

Though we don't use them in our example, you may have used the fieldset form component on your webform. Fieldsets also have Field Keys. To include the value of a form field that is inside a fieldset, you need to know both the Field Key of the fieldset and the form field. Let's say that our site_url field in the example was inside a fieldset which had a Field Key of site_information. The %email and %value tokens for this field would be:

  • %email[site_information][site_url]
  • %value[site_information][site_url]

Custom Email Subjects

You can also use tokens in the subject line of the email. For instance, entering this in the Custom field under E-mail subject

Web Feedback for %value[site_url]

will send an email with this subject line

Web Feedback for http://www.middlebury.edu
Powered by MediaWiki