Webforms in Drupal make for an easy way to get user submitted data. There are a handful of ways you can organize that data. You can even have the form submission data be sent to an email address. In some cases, you may want to have that data be sent to a different email address based on submitted data or a custom setting. Here we will go over one solution for getting webforms to submit submission data over email dynamically.
In this case, we'll start by setting up new webform and content type.
Setup the webform fields:
- Create a new webform
- Add the desired fields to the webform
- Add a hidden field (we'll call this "Email Override" or "field_email_override")
Set up conditional email handlers on the webform:
- Go over to the "Email/Handlers" tab and add a new Email
- Set the "To email" to a default value
- Go to the Conditions tab for this Email
- Set the "State" to "Enabled"
- Set the "Element" to "All"
- Add a "Trigger/Value"
- Select the "Email Override" field we created earlier and select the value as "Empty"
- Add a new "Email" in "Email/Handlers" tab
- Set the "To email" to the "Email Override" field in the select list
- Follow steps 4-6 for this Email setting
- Then select the "Email Override" field we created earlier and select the value as "Filled"
Set up the content type fields:
- Create a new content type
- Add a webform embed field and set the default value to the newly created form
- Add another textfield (we'll call this "Email Submissions To" or "field_email_submissions_to")
Now that our configuration is all set up, we can get to the code snippets that will actually drive the dynamic email submissions.
First we want to set up a hook_form_alter() implementation to get and check the email submissions to field value. In a custom module do this:
Now that code is setup to get and check the email submissions to field value, we need to process that value and pass it to the webform using the hidden field we created in the beginning.
With this, you should now be able to give your webforms the ability to dynamically set submission emails receiving address. This could be extended even further using taxonomy fields or select lists to get the email address. If you have any suggestions or other ways to accomplish this, please feel free to comment below.