Removing Fields from Webform Confirmation Email

By Paulus, 11 June, 2010

Scenario

Client wants to have Webform send out a confirmation email to the user who fills out a form.

Requirement(s)

  1. Tracking fields such as history can not be sent to the user.

Problem(s)

  1. By default, all form fields get submitted. Even the hidden fields. In the case of Visitorpath, this module adds several hidden fields for source, search terms, etc. If you are sending out confirmation emails it would be wise to hide that information from the user.

Solution

 

function phptemplate_preprocess_webform_mail_message(&$vars) {
  global $user;

  $vars['user'] = $user;
  $vars['ip_address'] = ip_address();

  // $vars['id'] is 1, then we are sending a confirmation email.
  if( count($vars['node']->webform['additional_emails']) > 0 && $vars['id'] == 1 ) {
  	unset($vars['form_values']['submitted_tree']['visitorpath_full_history']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_conversion_title']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_conversion_url']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_entrance_title']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_entrance_url']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_keywords']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_previous_title']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_previous_url']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_search_engine']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_source']);
  	unset($vars['form_values']['submitted_tree']['visitorpath_timeonsite']);
  }
}

The above function has been taken from the webform.module file and modified. The if statement was the only addition.  Since we are calling this function each time we send out an email (a total of two times) we need to know which one goes where. If $vars['id'] is set to '1' then the output of the proceeding functions will be sent to the user who filled out the form. However, if we are not sending a confirmation email for that pariticular form, then we need to know that. By counting the $vars['node']->webform['additional_emails'] array, if there is one or more email addresses here then we know that the webform has been set up to send out confirmation emails.