I've got a client who needs to separate out emails based on the issues. The issue options are in a pulldown menu for the user to choose. I need to be able to take the name of the issue and populate the subject line of the email that Webform sends out. Can I do that? I only see the option to use text fields. Is there a way to use PHP in a hidden field to set the subject or some other work around?

My client requires this option and I didn't realize that Webform couldn't do it until now. I'd rather not have to recreate the whole form.

Any help is greatly appreciated!

Comments

criznach’s picture

There's an article around here somewhere with a piece of PHP code you can post into a page node which creates a custom contact page. I've used it with some modifications to add checkboxes and validation to one of my sites. Here's the code I'm using. You should be able to customize the form array to include a select element.


$form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your Name'),
    '#default_value' => $object['name'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => TRUE,
);

$form['eMail'] = array(
    '#type' => 'textfield',
    '#title' => t('Your Email Address'),
    '#default_value' => $object['eMail'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => TRUE,
);

$form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => $object['subject'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => TRUE,
);

$form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Your Message'),
    '#default_value' => $object['message'],
    '#size' => 30,
    '#maxlength' => 2000,
    '#rows'    => 7,
    '#required' => TRUE,
);

$form['catalog'] = array(
	'#type' => 'checkbox',
	'#title' => t('Send me a catalog.'),
	'#default_value' => $object['catalog'],
);

$form['address'] = array(
    '#type' => 'textarea',
    '#title' => t('Address for Catalog'),
    '#default_value' => $object['address'],
    '#size' => 30,
    '#maxlength' => 200,
    '#rows'    => 3,
    '#required' => FALSE,
);

$form['copy'] = array(
	'#type' => 'checkbox',
	'#title' => t('Send me a copy of this message.'),
	'#default_value' => $object['copy'],
);
 
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('send email'),
);

$output = drupal_get_form('contactform', $form);
return $output;

// validation function for the contact form
function contactform_validate($form_id, $form_values) {
    // first we validate if there is a email injection
    $finds = array("/bcc:/i",
            "/Content-Type:/i",
            "/Mime-Type:/i",
            "/MIME-Version:/i",
            "/multipart\/mixed/i",
            "/boundary=/i",
            "/subject:/i",
            "/cc:/i",
            "/to:/i");
    foreach($form_values as $value)
          foreach($finds as $find)
                if(preg_match($find,$value))
                    form_set_error('', '<h2 class="red center">Stop spamming</h2>');

    // then we validate the email-adress     
    if (!valid_email_address($form_values['eMail']) && !empty($form_values['eMail']))
        form_set_error('', t('Please check the spelling of your email-adress.'));

    if ($form_values['catalog'] && empty($form_values['address']))
        form_set_error('', t('Please specify a mailing address for your catalog.'));
}


// submit function for the contact form
function contactform_submit($form_id, $form_values) {

	$message[] = t("!name has sent you a message from the !site website.", array('!name' => $form_values['name'], '!site' => variable_get('site_name', 'drupal')));
	$message[] = t('Message:');
	$message[] = $form_values['message'];
	if (isset($form_values['catalog'])) {
		$message[] = t("Please send me a catalog:");
		$message[] = $form_values['address'];
	}
	
	// Tidy up the body:
	foreach ($message as $key => $value) {
		$message[$key] = wordwrap($value);
	} 

	$copy[] = t("Thank you for visiting the !site website.  As requested, here's a copy of your message.", array('!site' => variable_get('site_name', 'drupal')));
	$copy[] = t('Message:');
	$copy[] = $form_values['message'];
	if ($form_values['catalog']) {
		$copy[] = t("Please send me a catalog:");
		$copy[] = $form_values['address'];
	}
	
	// Tidy up the body:
	foreach ($copy as $key => $value) {
		$copy[$key] = wordwrap($value);
	} 

    $from         = $form_values['name'].' <'.$form_values['eMail'].'>';
    $recipient    = 'Chris Miller <chris@xxx.com>';
    $subject      = $form_values['subject'];
	$message_body = implode("\n\n", $message);
    $copy_body    = implode("\n\n", $copy);
    $goto         = '<front>';

    if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
        $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
        drupal_set_message('<h3 class="red center">'.$output.'</h3>');
        drupal_goto($goto);
    }
	else {   

        user_mail($recipient, $subject, $message_body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");

		if ($form_values['copy']) {
			// Reply
			user_mail($from, $subject, $copy_body, "From: $recipient\nReply-to: $recipient\nX-Mailer: Drupal\nReturn-path: $recipient\nErrors-to: $recipient");
		}
   
        // Log the operation:
        flood_register_event('contact');
        watchdog('mail', t('%name-from use contact form', array('%name-from' => theme('placeholder', $form_values['name'] ." <$from>"),)));

        drupal_set_message('Your message has been sent');
        drupal_goto($goto);
        }

    }