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:
This is how the Form Components screen appears to an editor:
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:
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 |
| 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> |
|
<ul><li>First List Item</li><li>Second List Item</li></ul> |
|
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- This page was last modified on 8 December 2011, at 11:21.
- This page has been accessed 4,537 times.
- Privacy policy
- About LIS Documentation
- Disclaimers



