Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Webform Bonus Pack: email routing in webforms

Parent Feed: 

We would like to announce the creation of the Webform Bonus Pack module which allows you to do email routing and to send submissions digests in specific formats to a configured set of emails on cron.

This blog post aims to show different approaches of email routing (i.e.conditional emailing), to explain pros and cons of these approaches, and to show how to handle email routing efficiently using Webform Bonus Pack module.

Task statement

Let's assume we have a simple order webform for a Drupal webshop. It has four fields: Name, Email, What are you interested in, and Order details.

The What are you interested in field uses a multiple select component which have the following key-value pairs:

branding|Branding
graphic_design|Graphic design
web_design|Web design
website_support|Website support
website_performance|Website performance optimization
internet_marketing|Internet marketing
seo|Search engine optimization
hosting|Hosting

Assume we have following email routing rules:

Well known solutions

1. Webform's built in abilities

It is easy to send email to a custom address on every submission. Also we are able to send E-mail to an address which is coming from any of the webform components.

In our case we need to assign available values for the What are you interested in component to be like this:

[email protected]|Graphic design
[email protected]|Web design
[email protected]|Website performance optimization
[email protected]|Search engine optimization

As you can see it is easy to configure simple routing, however there are some limitations:

  • The key should be unique, hence it is impossible to use same email address for two or more keys.
  • It is impossible to send emails to multiple email addresses if a single item is selected.
  • Email addresses will be used instead of proper keys when viewing submission or downloading webform results.

As we can see, this is a fast solution, but it is not very configurable.

The Webform PHP module allows you to create a PHP component, which could be used to assign E-mail addresses to values (or whatever you want). In a PHP component you need to add specific PHP code that makes custom routing. Obviously in code you could read data from other components and use complex conditions. Although this is a highly configurable solution, it is very dangerous, so the module maintainer recommends against using this module unless the developer knows what he is doing.

3. Custom module

Using custom PHP code in your module for Webform validation or submission is more safe, and is also highly configurable, but it is more difficult to update routing rules and cannot be done easily by non-technical people.

Email routing using Webform Bonus Pack module

The Webform Bonus Pack module provides a mapping component, which can be easily used for email routing. Configuration is easy: you need to select the What are you interested in component as a mapped component and then add mapping key-pair values. Keys are the mapped component (e.g. What are you interested in) keys and values are emails. When items of the What are you interested in component are selected our routing component will return appropriate set of emails.

You need to select newly created Routing component as the E-Mail to address. And also don't forget to add [email protected] as custom email address to E-mails setting form.

Now you can test how it works: When you are submitting the form select the check-boxes for Website performance optimization and Hosting. Emails should then be sent to [email protected], [email protected], [email protected] and [email protected].

What's next?

I think that the mapping component will be evolving in the future. It would be good to have some functionality of or integration with the Rules module. Also, of course, I will be porting this to Drupal 7. In the next blog post about Webform Bonus module I am going to talk about another interesting feature, a submodule called Webform Digest, which already has integration with the mapping component.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web