I am using a webform module to create a contact us form. The customer has asked for a dropdown list for the visitor to select whom they would like to contact (web administrator/general enquiry). Is there a way to change the TO email depending on the selection? Or is there another way of achieving this?
4 Answers
Here's how I did it in Drupal 7, without the use of any other add-on modules:
Add a select box to the form.
In the select box options field, add your info in this format:
[email protected]|Select Option 1
[email protected]|Select Option 2
[email protected]|Select Option 3
Save your form, then click on the "Emails" sub-tab of the "Webform" primary tab.
Choose "Component Value > The name of the field you created in step 1". Click "Add".
Adjust your settings as needed on the following page, and you're done! The webform will now send an email to the address associated with the choice selected in the select box.
-
Do the keys (the e-mail addresses) appear in the
SELECT
? Commented Mar 5, 2013 at 21:36 -
Thanks @hockey2112 I'd rather not display the email addresses though...– kine456Commented Mar 5, 2013 at 23:08
-
The email addresses are not displayed in the drop-down menu, but they are displayed in the code of that drop-down. Commented Mar 6, 2013 at 0:39
-
SOrry to bother you again @hockey2112 Re: The webform will not send an email to the address associated with the choice selected in the select box DId you mean to say WILL or will not send an email?– kine456Commented Apr 2, 2013 at 1:35
-
Sorry, it should have said "will NOW send...". I just edited my answer to correct that issue. Commented Apr 2, 2013 at 14:43
You can achieve this with the Webform rules module. You can catch every webform submit and then apply actions on it depending on the submit values. This module helps exposing webform events to the rules module.
The Rules module is used to trigger actions on defined events, so for example when a webform is submitted you can create a condition (in Rules) that checks the dropdown value and then trigger an action that sends the mail accordingly. You can have as many rules as diferent recipients.
-
Thank you @drcelus These rules, is that something I would set in my webform tab? I haven't been able to find it as yet, and the webforum rules readme file isn't making much sense to me.– kine456Commented Mar 6, 2013 at 0:03
-
Sorry for not being clear enough, the module I mention is meant to be used with the Rules module. Rules is used to trigger actions on events, see my edit on the answer.– drcelusCommented Mar 6, 2013 at 6:43
Build your own module, hook_form_alter($your_forms_id) and add your own submit handler like
/**
* Implements hook_form_alter().
*/
function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'YOUR_forms_id'){
$form['#submit'][] = 'my_submit_';
}
}
PS:Your default (webform) submit handler is executed also ..
function my_submit_($form, &$form_state) {
drupal_set_message('success!')
...
drupal_mail($form_state['values']['your element'])...
}
Have a look at issues [#800350] or [#1026758]. In short: you can do so by adding the following (templated) code:
/**
* Implements hook_mail_alter
* @see http://api.drupal.org/api/drupal/modules--system--system.api.php/functio... *
* @param array $message
*/
function mymodule_mail_alter(&$message) {
if ($message['id'] == 'webform_submission') {
if (isset($message['params']['node']) && isset($message['params']['submission'])) {
$node = $message['params']['node'];
$submission = $message['params']['submission'];
// do your work, e.g. changing $message['from'], $message['to'], etc.
...
}
}
}
By using hook_mail_alter you will also catch resubmissions, that most other solutions don't.