Sending Multiple Customized Confirmation Messages with Webform.Module
The Webform module for Drupal allows for the creation of customized confirmation e-mail messages to be sent after a user fills out a form. This can be accomplished via some simple pointing and clicking in the "Webform email settings" fieldset when editing the form. However, if you need to send out more than one customized message (say one message to the user, and one message to the site administrators), you'll need to utilize the "Additional Processing" feature of the Webform.
The code below loads values from the submitted form, creates a customized message body, defines a hook_mail() callback to complete the message, and then sends the message via drupal_mail(). This code should be added to the "Additional Processing" area (under the "Webform advanced settings" fieldset).
<?php
// Variables we might need from the form
$submitted_email_address = $form['submitted']['email_address']['#value'];
$submitted_first_name = $form['submitted']['first_name']['#value'];
$submitted_last_name = $form['submitted']['last_name']['#value'];
// Mail setup
$to = $submitted_email_address;
$from = variable_get('site_mail', 'default.address@examplesite.com');
// Use this method to load the confirmation message from the webform itself
//$body = drupal_html_to_text($node->webform['confirmation']);
// Use this method to define a custom confirmation message
$body = "
Dear $submitted_first_name $submitted_last_name,
Thanks for your interest in our site. Feedback from users is very important to us.
Talk to you soon,
The Web Team
"
;
// Define a hook_mail() callback which drupal_mail() will use to complete the $message structure
function webform_extra_mail($key, &$message, $params) {
$message['subject'] = "Thanks for contacting us!";
$message['body'] = $params['body'];
}
// Theme and send the message via drupal_mail()
$message = drupal_mail('webform_extra', 'reply', $to, language_default(), array('body' => $body), $from, TRUE);
?>
Comments
#1 Credit Where Credit Is Due
Submitted by Eric Weik on Thu, 07/02/2009 - 12:08.
I've been using this particular block of code for a while now, but searching around a bit, it probably originally came from this thread on Drupal.org: Sending confirmation email.
#2 Confirmation email not sending
Submitted by Visitor (not verified) on Mon, 12/07/2009 - 19:09.
Below is the code I have entered. It will send the email to admin that someone has submitted the form, but I cannot get the module to actually send out the confirmation module to the person submitting the form.
Any help would be greatly appreciated!
I am using Drupal 6.
Thanks!
<?php$email_address = $form_state['values']['submitted_tree']['email_address'];
$name = $form_state['values']['submitted_tree'['your_name']; $to = $email_address;
$from = variable_get('site_mail', ''); // Use this method to load the confirmation message from the webform itself
//$body = drupal_html_to_text($node->webform['confirmation']);
// Use this method to define a custom confirmation message
$body = "Dear $name,
Thanks for your interest in our site.
Talk to you soon,
;The Team
"
function
webform_extra_mail($key, &$message, $params) {$message['subject'] = "Thanks for contacting us!";
$message['body'] = $params['body'];
} $message = drupal_mail('webform_extra', 'reply', $to, language_default(), array('body' => $body), $from, TRUE);
?>
#3 Try $form or $form_values
Submitted by Eric Weik on Mon, 12/07/2009 - 20:00.
I'd have to look to be sure, but I think that you might want to use the $form[] array here instead of $form_state[].
So from your example, try replacing your first two lines with:
<?php$email_address = $form['submitted']['email_address']['#value'];
$name = $form['values']['your_name']['#value'];
// ... rest of your code here ...
?>
If you have the devel module installed, you can also examine the available variables with something like this:
<?phpdpm($form);
dpm($form_values);
?>
#4 Fixed
Submitted by Visitor (not verified) on Tue, 12/08/2009 - 17:36.
Thanks for help, I got it working :-)
#5 Thank You!
Submitted by Steve Kessler (not verified) on Thu, 01/14/2010 - 14:32.
Your code worked perfectly for my very simple use case.
Thank you very much for supporting the community!
-Steve
Post new comment