I'm a relative newcomer to Drupal and PHP, and I have put together a Drupal site for a small high school, where we wish to use webforms to allow teachers to contact other teachers regarding specific students. So far, webform module seems very promising for this, however it would be very handy if it were possible to optionally CC additional support staff from the webform.

Any thoughts on how to accomplish this? I tried tinkering with adding a cc address variable and form field to the webform module, and modified the user_email function (in user.module) to accept a cc address variable, but I don't know how to get the mailserver to understand how to handle a cc. How can the user_email function be re-written to enable optional cc-ing without messing up the other cases where user_email is used for site administration or user contact?

Comments

erdemkose’s picture

You should add you CC to the header of the mail. It must not contain line breaks.

CC: somebody@drupal.org, somebody@gmail.com, .....

CC(Carbon Copy) can also be seen by each of the recipients. If you don't want this to happen, then use BCC(Blind Carbon Copy).

I am not using webform or user_email modules so I have no specific things to say for these modules.

--------------------------------------------------------------
http://www.students.itu.edu.tr/~koseer/drupal/ - works on PHP-TXT-DB database layer.

Andrupal’s picture

Thanks for the tip. I got CC to work by doing the following

1) Added field email_cc to the webform database using phpmyAdmin

2) Changed this section

 // Lazy Initializer

  db_query("INSERT INTO {webform} (nid, confirmation, email, email_from, email_cc, email_subject) VALUES (%d, '%s', '%s', '%s', '%s')",
           $node->nid, $node->confirmation, $node->email, $node->email_from, $node->email_cc, $node->email_subject); 

3) And this section

 // Lazy Initializer
  _webform_database_lazy_update();
  
  db_query("UPDATE {webform} SET confirmation = '%s', email = '%s', email_from = '%s', email_cc = '%s', email_subject = '%s' ".
           "WHERE nid = %d", $node->confirmation, $node->email, trim($node->email_from), trim($node->email_cc), trim($node->email_subject), $node->nid);  

4) And this section

// Lazy initializer
  
  _webform_database_lazy_update();
  
  $page = db_fetch_object(db_query("SELECT confirmation, email, email_from, email_cc, email_subject FROM {webform} WHERE nid = %d", $node->nid)); 

5) And this section

// Build arrays of possible return email addresses and email subject lines from elements on the form
	$possible_email_from = array('Automatic' => 'Automatic');
        $possible_email_cc = array('Automatic' => 'Automatic');
	$possible_email_subject = array('Automatic' => 'Automatic');

  if( is_array($node->webformcomponents_name) && !empty($node->webformcomponents_name)) {
    foreach($node->webformcomponents_name as $key => $name) {
    	$type = $node->webformcomponents_type[$key];
			if( $type == 'email' || $type == 'hidden' ){
				$possible_email_from[$name] = $name;
			}
                        if( $type == 'select' || $type == 'hidden' ){
				$possible_email_cc[$name] = $name;
			}
			if( $type == 'textfield' || $type == 'hidden' ){
				$possible_email_subject[$name] = $name;
			}
		}
	}
  $output .= form_select(t('E-mail CC address'), 'email_cc', $node->email_cc,
                         $possible_email_cc, t('Form e-mails will have these options for CC addresses.'));

  $output .= form_select(t('E-mail from address'), 'email_from', $node->email_from,
                         $possible_email_from, t('Form e-mails will have this return address. Choose Automatic for the default'));

  $output .= form_select(t('E-mail subject'), 'email_subject', $node->email_subject,
                         $possible_email_subject, t('Form e-mails will have this subject line. Choose Automatic for the default'));
  
  return $output;
}

Note the above is for a limited list of "select" style options, and CANNOT be used with multiple select. All select options must be email addresses separated by commas. An alternative option, that would leave you more vulnerable to spam, would use a textfield.

6) And this section:

 // Handle posting to the form and output status.
  $reply = $_POST['edit']['submitted'];
  $message = _webform_create_mailmessage($reply);
  
  if (isset($node->email)) {
    $node->email = strip_tags($node->email);
    if (!empty($node->email)) {
      // Build arrays of possible return email addresses and email subject lines from elements on the form
      // This is a duplicate of the code that creates the arrays for selection.
      $email_from_string = "From: ". mime_header_encode(variable_get('site_mail', "webmail@{$_SERVER['SERVER_NAME']}"))."\n";
      $email_subject_string = t('Form submission from: ').' '.$node->title;
      
      if( is_array($node->webformcomponents_name) && !empty($node->webformcomponents_name)) {
        foreach($node->webformcomponents_name as $key => $name) {
          $type = $node->webformcomponents_type[$key];
          //$value = $node->webformcomponents_value[$key];
          if( $type == 'email' || $type == 'hidden' ){
            if($name == $node->email_from){
              $email_from_string = "From: ".mime_header_encode(strip_tags("$reply[$name]"));
            }
          }
            if( $type == 'select' || $type == 'hidden' ){
            if($name == $node->email_cc){
              $email_cc_string = "CC: ".mime_header_encode(strip_tags("$reply[$name]"));
            }
          }
          if( $type == 'textfield' || $type == 'hidden' ){
            if($name == $node->email_subject){
              $email_subject_string = strip_tags("$reply[$name]");
            }
          }
        }
      }
      
      $headers .= "Date: ".date("r")."\n";
      $headers .= "X-Mailer: Drupal Webform (PHP/" . phpversion().")\n";
      $headers .= $email_cc_string."\n";
      $headers .= $email_from_string."\n";

      // START - SPAM FILTER
      // check if they are spamming using a bcc hack
     // if (preg_match('/b*cc\s*:/i', $node->mail)
       //   || preg_match('/b*cc\s*:/i', $email_subject_string)
       //  || preg_match('/b*cc\s*:/i', $message)
       //   || preg_match('/b*cc\s*:/i', $headers)
       //   ) {
      //  drupal_set_message(t('The string \'BCC:\' is banned.'), 'error');
      //  return FALSE;
     // }
      

Note, commenting out the above leaves one vulnerable if webforms are used by anonymous users. For me this isn't an issue, as they are only used internally.

7) Make a webform and set up a CC select field that contains the email addresses you want your users to select from.

To do: Ideally, I wanted to allow checkboxes, with the ability for multiple CC selections. This doesn't work given the way this module returns multiple choice information. Instead of a comma-separated string, the module returns a table of selected values like this:

0 = choice 1
1 = choice 2
2 = choice 3
etc.

Perhaps someone knows how to alter this?

Andrupal’s picture

Change to

 // Lazy Initializer

  db_query("INSERT INTO {webform} (nid, confirmation, email, email_from, email_cc, email_subject) VALUES (%d, '%s', '%s', '%s', '%s', '%s')",
          $node->nid, $node->confirmation, $node->email, $node->email_from, $node->email_cc, $node->email_subject);

Note: added a fifth '%s' to the VALUES argument.

sillygwailo’s picture

Webform CC adds the option to CC email addresses on the emails sent out by the Webform module. A D7 version is forthcoming.

(Username formerly my full name, Richard Eriksson.)