Webform Module – How to send a confirmation email
After spending a couple hours trying to figure out the best way to send a confirmation email after a submission using the Webform Module, I came upon the solution here: http://drupal.org/node/323666#comment-1119996
The code that ended up working for me was as follows (many thanks to grendzy):
$to = $form['submitted']['email']['#value'];
$from = variable_get('site_mail', '');
$body = drupal_html_to_text($node->webform['confirmation']);
$message = drupal_mail('webform_extra', 'reply', $to, language_default(), array('body' => $body), $from, TRUE);
function webform_extra_mail($key, &$message, $params) {
$message['subject'] = "Thanks for your interest.";
$message['body'] = $params['body'];
}This code gets inserted into the “Additional Processing” field on the webform’s edit page. Any code placed in this field will be executed upon form submission.
Not every form is built the same, but in the case above it is looking to fill the $to variable with a value submitted as part of the form. The value it’s looking for is called ‘email’ and can be found within the $form['submitted'] variable. In the case that I had to implement, the email variable was placed inside a fieldset called ‘contact_information’, leaving the value I needed to reference:
$to = $form['submitted']['contact_information']['email']['#value'];To dig out the proper method to reference these variables, I simply printed out the $form array and placed and exit() call. Looking at the page source allowed me to see the entirety of the $form object. Some people use the Devel module and subsequently use the dsm() function to accomplish the same thing.
