Last updated September 6, 2013. Created by batigolix on June 23, 2008.
Edited by ianthomas_uk, danielhonrade, quicksketch. Log in to edit this page.

To use the conditional recipient you add a webform component of the type "select".

While building the webform:

  • Click the "Webform" tab that lists components in the form
  • Enter name of field, e.g. "Kind of inquiry"
  • Choose "select"
  • Click "add"

The "Edit component" screen appears

  • Enter a Label and Field key e.g. "Kind of inquiry", "kind_inquiry"
  • In the field "options" add a couple of key-value pairs like

info@example.com|General information
johnpaul@example.com|Sales question
joseph@example.com|Technical question

The user will be presented the readable option while the key remains invisible.

  • Save your changes
  • Select "Configuration" and choose "Kind of inquiry" under "Conditional e-mail recipients"

After the component has been set up, you now need to configure the e-mail.

  • Under the "Webform" tab, click the sub-tab for "E-mails".
  • Choose the component you set up with e-mail keys to be the recipient and click "Add".
  • Configure the e-mail setting as needed and save.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Maybe I'm missing something here, but isn't the email still readable in the source code? I'd like to avoid having readable emails in the source, even though they aren't visible to a typical form user.

To be clear: At this writing (January 2011), this technique still results in email addresses being stored in clear text in the HTML. Email-harvesters will find them without a problem.

I've used webform successfully in other places. I'm a bit stymied on this one.

I have a select list with five items:
user1@place.com|one
user1@place.com|two
user2@place.com|three
user2@place.com|four
user2@place.com|five

The form displays for the selection field.
two
five

I have the same issue where I have various countries (in the select list) associated with the same email. So, if region one and region two have the same email address only the first option (region one) appears in the select list.

I found this website which allow webforms to use the same key value for multiple options.

http://www.interworks.com/blogs/gbivens/2010/06/04/tricking-drupal-webfo...

Here is an example:

usa@example.com|Oklahoma City - June 2010
usa@example.com |Tulsa - July 2010
usa@example.com |Stillwater - July 2010
gb@example.com|London - August 2010
gb@example.com |Manchester - September 2010

scratch that

I recently ran into both of the issues commented on above (that recipient addresses must be unique, and addresses are exposed in the page markup).

My solution was to use hook_mail_alter() to set up a list of email "aliases" (such as "orders_at_example"), that are replaced with actual email addresses when they appear in the to: field when Webform sends the message via drupal_mail().

So in Webform you set up a select with something like this:

orders_at_example|Orders
customer_service_at_example|Customer Service
technical_support_at_example|Technical Support

And then in hook_mail_alter() you replace "orders_at_example", "customer_service_at_example", and "technical_support_at_example" with the actual email address (or addresses).

So a really simple example would look like the following:

<?php
/**
* Implementation of hook_mail_alter().
*/
function emerj_overrides_mail_alter(&$message) {
 
$search = 'orders_at_example';
 
$replace = 'orders@example.com';
 
$message['to'] = str_replace($search,$replace,$message['to']);
}
?>

I've posted a more detailed explanation and example using hook_mail_alter() on my blog if anyone needs it.

-Eric

I recently ran into this issue as well, and after reading your info about how it doesn't work w/webform 3.x, I came up with this solution:

I needed to have a staff contact form that automagically sent the email to the proper staff, but I didn't want to expose the email addresses on the page. I wanted to allow the customer to enter the email addresses themselves, so I created a taxonomy called 'staff_email' and added an email field to that so they can add a key and the associated email. Then when the webform is submitted, I replace the key with the associated taxonomy term.

The basic parts were using hook_form_alter (or more specifically hook_form_FORM_ID_alter in my case) to add an additional submit function to the form submit, and also to retrieve the select options from the taxonomy for the form display:

<?php
function MYMODULE_form_webform_client_form_51_alter(&$form, &$form_state, $form_id) {
 
// add additional submit processing before the standard stuff
 
array_unshift($form['#submit'], 'MYMODULE_staff_contact_pre_submit');
 
$option = array();
 
// retrieve the staff_email taxonomy
 
$vid = get_vid_from_name('staff_email');
 
$tax = taxonomy_get_tree($vid);
 
// assemble the option items
 
foreach ($tax as $tax_object) {
   
$key = $tax_object->name;
   
$value = ucwords(str_replace('_', ' ', $key));
   
$option[$key] = $value;
  }
 
// set the form field(s)
 
$form['submitted']['recipient']['#options'] = $option;
}
?>

The additional submit processing function that looks up the taxonomy term and replaces it w/the staff member's email address:

<?php
function MYMODULE_staff_contact_pre_submit($form, &$form_state) {
 
$recipient = $form_state['values']['submitted']['recipient'];
 
// replace w/the correct email
 
$email = MYMODULE_get_staff_email($recipient);
 
$form_state['values']['submitted']['recipient'] = $email;
}
?>

And a couple helper functions:

<?php
function get_vid_from_name($name) {
 
$vocab = taxonomy_vocabulary_get_names();
  if (isset(
$vocab[$name]->vid)) {
    return
$vocab[$name]->vid;
  }
  return
NULL;
}
function
MYMODULE_get_staff_email($term_name) {
 
$vid = get_vid_from_name('staff_email');
 
// retrieve full taxonomy
 
$tax = taxonomy_get_tree($vid, 0, NULL, TRUE);
 
// find the one we're looking for
 
foreach ($tax as $i => $term) {
    if (
$term->name == $term_name) {
      return
$term->field_email['und'][0]['value'];
     
// where does taxonomy get it's language from?  if this was a node it would be there but
      // didn't see the language in the tax term... hmmm.
   
}
  }
 
// if no recipient was found, send to a default email addres
  // (this shouldn't really happen, but I suppose it's possible if
  // no email address/key was entered for someone(?))
 
$default_email = 'default@email_address.com';
  return
$default_email;
}
?>

Furthermore, probably a "better" way to populate the options list would be an implementation of hook_webform_select_options_info(), instead of doing it in hook_form_FORM_ID_alter.

Updates to my above code to do this would look something like this:

<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function MYMODULE_form_webform_client_form_51_alter(&$form, &$form_state, $form_id) {
 
$recipient_index = 1;
 
// add additional submit processing before the standard stuff
 
array_unshift($form['#submit'], 'MYMODULE_staff_contact_pre_submit');
}
/**
* Implements hook_webform_select_options_info().
*/
function MYMODULE_webform_select_options_info() {
 
$items = array();
 
$items['staff_email_taxonomy'] = array(
   
'title' => t('Staff Email Taxonomy'),
   
'options callback' => 'MYMODULE_webform_options_staff_email_taxonomy',
  );
  return
$items;
}
// builds the webform options from the staff_email taxonomy
function MYMODULE_webform_options_staff_email_taxonomy() {
 
$option = array();
 
// retrieve the staff_email taxonomy
 
$vid = get_vid_from_name('staff_email');
 
$tax = taxonomy_get_tree($vid);
 
// assemble the option items
 
foreach ($tax as $tax_object) {
   
$key = $tax_object->name;
   
$value = ucwords(str_replace('_', ' ', $key));
   
$option[$key] = $value;
  }
  return
$option;
}
?>

Same idea, just moves the options code to the "proper" place :)

I recently re-visited my code and found a better way to do this, without using the taxonomy. This uses the Node Id as the value for the option, and then pulls the email from the email field on the node when the form is submitted:

<?php
function MYMODULE_webform_select_options_info() {
 
$items = array();
 
$items['staff_email'] = array(
   
'title' => t('Staff Email'),
   
'options callback' => 'MYMODULE_webform_options_staff_email',
  );
}
?>

<?php
// builds the webform options from the staff content type
function MYMODULE_webform_options_staff_email() {
 
$option = array();
 
$result = db_select('node', 'n')
    ->
fields('n', array('nid', 'title'))
    ->
condition('n.type', 'staff')
    ->
condition('n.status', '1')
    ->
execute()
    ->
fetchAllAssoc('nid');
  if (isset(
$result) and is_array($result) and count($result)) {
    foreach (
$result as $staff) {
     
$option[$staff->nid] = $staff->title;
    }
  }
  return
$option;
}
?>

<?php
// perform some actions on the staff contact form info before it's
// submitted to the standard drupal submit
function MYMODULE_staff_contact_pre_submit($form, &$form_state) {
 
$recipient = $form_state['values']['submitted']['recipient'];
 
// replace w/the correct email
 
$email = MYMODULE_get_staff_email($recipient);
 
$form_state['values']['submitted']['recipient'] = $email;
}
?>

<?php
// retrieves the staff email from the node
function MYMODULE_get_staff_email($nid) {
 
// set a default email address
 
$returnvalue = variable_get('site_mail', 'default@email.com');  // use site email as default, or set something if no site email is set
 
$node = node_load($nid);
  if (isset(
$node->field_email)) {
   
$returnvalue = $node->field_email[$node->language][0]['value'];
  }
  return
$returnvalue;
}
?>

The emails are kept hidden from the form source, and you don't have to enter anything extra after entering the info for the node / custom content type.

Hello friend,
is it posssible to get the data ( columns email id, state) from the database and to display it in the webform select component.?? then i will use Conditional e-mail recipients for this one.. i am struck with how to get the database content. If you know , plz informe me.. Thanks in advance.

< ---- Maddy ----->

I ran into this issue and didn't want to make user email addresses public either, so I created a reusable module that effects fake email address aliases. So in an admin panel you can just enter aliases to reuse across your site (including the aforementioned webform options).

Say you want to protect Amy, Bob, Carla and Doug's email addresses in a webform with conditional email recipients:

  • For Email Aliases at /admin/settings/email-alter you'd enter:
    corporate@example.net | Bob@example.net, Carla@example.net, Doug@example.net
    meetings@example.net | Amy@example.net, Bob@example.net
    legal@example.net | Carla@example.net
    help@example.net | Amy@example.net, Bob@example.net, Carla@example.net
  • In the webform select options you'd enter:
    corporate@example.net|Corporate
    meetings@example.net|Meetings
    legal@example.net|Legal
    help@example.net|Help

I posted my module at https://github.com/msmithgu/email_alter for anyone that runs into a similar issue.

Thank you very much for the module! It works very good. Only one thing... Is it possible to have one recipient in every email in the "To" field? Not the full list?

can you have more than one email address per option?
So what If i wanted

example@email.com, example2@email.com| No

(no being a check box)

but I think only as long as they are unique

So..

person1@foo.com,person2@foo.com|Apples
person3@foo.com|Pears
person4@foo.com|Grapes

but not

person1@foo.com,person2@foo.com|Apples
person2@foo.com|Pears
person3@foo.com|Grapes

at least not without using hook_mail_alter()

hth,
Anne

Strange if it is working? I found this in the issue queue indicating that it should not be working:
#339416: Multiple email address for an option in the Options field?

Webform does not allow multiple e-mail addresses within a single select list option. It does allow multiple e-mail address to be used on the main e-mail configuration options, which is accessible only to users that may edit the actual Webform node.

EDIT: Well it seems that Webform does support multiple e-mails in select lists or hidden fields. The current code clearly seems to support it.

The + trick would not work for us but I have no time to do custom modules for our launch. Here is a simple solution (but still leaves email addresses for harvesting). Make the values unique by adding a number to what would be duplicate values...

lewis@x.com,cole@x.com,board@x.com|Board of Directors
webmaster@x.com,custsvc@x.com|Web Site Issues
webmaster@x.com,custsvc@x.com,1|Online Orders
custsvc@ex.com| Customer Service
custsvc@x.com,1| Key Contact Program
jobs@x.com|Employment/Jobs
custsvc@x.com,2|Recycled Cheese
recreate@x.com|Recreation
wtrcserv@x.com|Cheese Conservation Rebate Programs

btw - this is not my idea I am just passing it along and seemed to work for us on Acquia Drupal v6 iis7 win2008 w/Exchange as mailserver.

As far as I can tell, this does not work.

Thanks, this does seem to work for me. Drupal 7, Webform 3. Also, when testing, don't forget to check you spam folder!

Is there a way to implement this as shown, but store the value (e.g., General information) in the database rather than the key (info@someorganisation.va)? I'm sure it's a little snippet under Additional Processing... but this seems like a pretty common use case.

I'm specifically thinking about the Table and Download report views...

Is it possible to avoid sending information to all recipients of the selected values that are not related to their e-mail?

Examle:

bob@mail.com|newsletter on music
anne@mail.com|newsletter on non-fiction
peter@mail.com|newsletter on fiction

A user selects newsletter on music and newsletter on non-fiction and presses "Send form"

Now both Bob and Anne gets an e-mail where they can see both selections.
I would like bob to only see that the user has selected the newsletter on music which is relevant to him.

I need someone to put me out of my misery...unlike everyone else, I seem to be having trouble with something super basic - finding the "Configuration" link.

It's this step:
Select "Configuration" and choose "Kind of enquiry" under "Conditional e-mail recipients"

I mean, I assumed it meant the regular configuration in the main admin menu, but, the only option I have there is Content Authoring > Webform Settings. And I can't find anything about "Conditional Email Recipients." This is the same page that links to the "Configure" option beside the webform module on the modules menu item.

I am begging someone to just tell me what the heck I should click after saving my select list. I'm back on the webform tab that displays all the fiels on my webform and the options that I have are "Form components," "emails," and "form settings."

On, and I'm using D7 with the latest release of webform.

Apparently in D7, you can just skip this step.

I added in the key/values as demo'ed and then added that component under the Emails button. It sends it to the value entered, no problem. But I didn't specify anything about "Conditional Emails" anywhere. Is this just how it's done in D7 or am I missing something?

Hi everyone,

Did this get solved? I also wanted to use multiple emails for different departments in a select list but that functionality is no use if the emails are printed in the source code.

Sam.

This is an issue I'm looking for solution too as well.

See my above post for an example of how to do this using a taxonomy to store the email addresses (and not expose the email in the source of the page)
http://drupal.org/node/273806#comment-6169606

I've updated my method above so it no longer uses the taxonomy, so no extra info is required to be entered. Just create an email field on your custom content / node, and using the code it pulls it out of the node when the form is submitted. The node id is used as the value so no email address appears in the source.

I have a simular request to use a select box to send to multiple email addresses.

See my above post for an example of how to do this using a taxonomy to store the email addresses (and not expose the email in the source of the page)
http://drupal.org/node/273806#comment-6169606

I believe this problem is being over thought.

Simply perform a hook on webform submission presave and provide a conditional to check against the users submitted value.

<?php
/**
* Implements hook_webform_submission_presave().
*/
function MYMODLE_webform_submission_presave($node, &$submission) {
  if (
$submission->data[10]['value'][0] == 'SOMEKEY') {
   
$submission->data[10]['value'][0] = 'oneemail@here.com';
  }
  else {
   
$submission->data[10]['value'][0] = 'adifferentemail@here.com';
  }
 
// Make sure you include the intended recipients, otherwise this can end up causing duplicate sends.
 
webform_submission_send_mail($node, $submission$submission->data[10]['value'][0]);
}
?>

The select option would of course need to be the component value on the email settings page.

Hope this helps,

This works- and so easy. Thanks!

-TWood