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.
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:
website_performance|Website performance optimization
seo|Search engine optimization
Assume we have following email routing rules:
- The webform should send every submission to firstname.lastname@example.org.
- If the user selects Graphic design or Branding it should send email to email@example.com.
- If the user selects Web design or Website support it should send email to firstname.lastname@example.org.
- If the user selects Website performance optimization it should send email to both email@example.com and firstname.lastname@example.org.
- If the user selects anything else submission should be sent to email@example.com.
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:
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.
2. Webform PHP module
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 firstname.lastname@example.org 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@example.com, firstname.lastname@example.org, email@example.com and firstname.lastname@example.org.
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.