Conditional Email Recipients with Webform

Building on an earlier post about multiple email recipients, we are now going to see how to add multiple recipients to a single email, when webform is setup to send emails to multiple people. Here is an example setup. Two people within the organization are receiving the email submission and the visitor is receiving a confirmation message. The confirmation uses a different template from the internal messages. I used to send the confirmation message using Rules, but something changed with my configuration, causing the emails send to Rules to be obfuscated by spamspan. I was unable to find a solution within an acceptable amount of time and needed to find another solution. This is also something that can be handled within webform without relying on Rules, so I see it as an overall win.

Webform email configuration screen

I would also like to add a carbon copy to one of the internal messages. Based on my previous post, the headers that we have added will be included with all emails, the internal and external recipients. After printing out $variables for the THEME_webform_mail_headers() function in our template.php, you will see that there is an 'email' array that contains information specific to the email being sent. Here are all of the values available:

email array values

I chose to evaluate for the eid parameter, this is machine number given to each email in the list. This way, even if I change the email value the process will still work as intended. We can now add this check to THEME_webform_mail_headers() in template.php:

function THEME_webform_mail_headers($variables) {
  $headers = array();

  if ($variables['node']->nid == '1234' &&
      $variables['email']['eid'] == '1') {
    $headers['cc'] = 'person4@example.com,person5@example.com';
    $headers['bcc'] = 'person6@example.com';
  }

  return $headers;
}