Drupal Quick Tip: Webform Email to Multiple Email Input Fields

Posted on July 7, 2017 in Drupal, Tutorial

Because I want to remember a) that it’s possible to send an email to multiple email fields and b) remember how to do it.

The Goal

The idea is pretty simple, but here’s the use-case I came across this week. I have a Drupal 7 client website that has a contact form using the “Webform” module. That contact form needs to be completed by a site visitor and the contents need to be emailed to a handful of people. That handful of people changes based on the store location page on which the form is submitted. With me so far?

So.. one Webform block set to show on lots of store locations pages and then emails need to be sent to people related to that location.

How To….

To achieve this, I created a handful of email fields on the Webform that are pre-populated with the email addresses for that location. Those fields are then hidden from view – no need for people to spam them directly.

As you can see, this pharmacy contact form needs to be emailed to the VP Operations, Pharmacy Manager, and Director of Sales. Those fields have been pre-populated, but only one email needs to be sent to all of them. (That’s the tricky part….)

By default, you can choose to type in a custom email address or choose one of the email fields from the “Component” drop-down, but I want to send one email to all 3 of those folks. To do that, I’ll leverage the “Token” module – which is probably already installed on your site since it’s a dependency for lots of other modules.

The “Token” module provides the ability to use strings to represent dynamic content. In this case, we’ll use them to represent the three email address fields.

Under the Webform tab labeled “Emails”, you can start to create a new email for this form’s submissions. I’ll assume you know how to do that already. Once you’re in and configuring the email, there will be a “Browse available tokens.” link below the email body field, but before the “Save” button.

When you set up the webform, you gave each field a name and a machine name. This is important to know. If you need to go back and get the machine names for the email fields, do that now.

In this new window, you’ll see there is an option to include a value from a form field using the token: [submission:values:?]

(the question mark is where the machine name of the field goes…) To include a value from the form in your email body, you can use the token and the machine name of the field you’re referencing, but this can also be done in the email “To:” field above!

So… to include all three email addresses in the “To:” field, I’ve placed this code in the field – making sure to separate them with commas: [submission:values:vp_operations_email],[submission:values:pharmacy_manager_email],[submission:values:director_of_sales_email]

Now, when the form is submitted, all three email addresses are emailed the same submission email!

Leave a comment

Your email address will not be published. Required fields are marked *