Skip to main content

Blog Post

Dynamic Email Submissions for Webforms in Drupal 8

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:

  1. Create a new webform
  2. Add the desired fields to the webform
  3. Add a hidden field (we'll call this "Email Override" or "field_email_override")
     

Set up conditional email handlers on the webform:

  1. Go over to the "Email/Handlers" tab and add a new Email
  2. Set the "To email" to a default value
  3. Go to the Conditions tab for this Email
  4. Set the "State" to "Enabled"
  5. Set the "Element" to "All"
  6. Add a "Trigger/Value"
  7. Select the "Email Override" field we created earlier and select the value as "Empty"
  8. Add a new "Email" in "Email/Handlers" tab
  9. Set the "To email" to the "Email Override" field in the select list
  10. Follow steps 4-6 for this Email setting
  11. Then select the "Email Override" field we created earlier and select the value as "Filled"
     

Set up the content type fields:

  1. Create a new content type
  2. Add a webform embed field and set the default value to the newly created form
  3. 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:

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
  // Use regex to make sure we are using the correct form.
  if (preg_match('/webform_submission_my_webform_node_/', $form_id)) {
	// Get the node for the page the webform is on.
	$node = \Drupal::routeMatch()->getParameter('node');
	// Check that this node is valid.
	if ($node instanceof \Drupal\node\NodeInterface) {
  	// Get the value of the email submissions to field.
  	$email_override = $node->get('field_email_submissions_to')->getValue();
  	// Check that the value exists.
  	if (!empty($email_override)) {
    	// Form values for webform can not be set in hook_form_alter(), so pass to a validation function.
    	$form['actions']['submit']['#validate'][] = 'my_module_email_override';
  	}
	}
  }
}

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.

/**
 * Changes the TO email value depending on the parent node's email submission
 * field value.
 *
 * @param array $form
 *   Form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   From state from the form.
 */
function my_module_email_override($form, FormStateInterface $form_state) {
  // Get the node for the page the webform is on.
  $node = \Drupal::routeMatch()->getParameter('node');
  // Check that this node is valid.
  if ($node instanceof \Drupal\node\NodeInterface) {
	// Get the value of the email submissions to field.
	$email_override = $node->get('field_email_submissions_to')->getValue();
	// Check that the value exists.
	if (!empty($email_override)) {
  	// Set the value of email submissions to field to the email override field on the webform.
  	$form_state->setValue('email_override', array_shift($email_override)['value']);
	}
  }
}


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.

Additional Resources
Theming a Webform in Drupal 7 | Blog
Good Form: 5 Tips for a Smooth Sign-Up Process | Blog
Configuration Management in Drupal 8 | Blog

Chris Runo

Meet team member, Chris Runo

Chris brings four years of Drupal experience to his role as a Senior Drupal Developer at Mediacurrent. Throughout his web development career, Chris has gained a unique perspective on what...

Learn more about Chris >
Access icon Up arrow icon Drupal 8 icon Facebook icon - white Facebook icon - blue outline Facebook icon - yellow Hollow right arrow icon Hollow right arrow icon - white LinkedIn icon - white LinkedIn icon - hollow LinkedIn icon - blue outline LinkedIn icon - yellow Mediacurrent wordmark Quote icon Twitter icon - white Twitter icon - hollow Twitter icon - blue outline Twitter icon - yellow Youtube icon - white Youtube icon - yellow