Last updated March 19, 2010. Created on October 20, 2008.
Edited by nitrospectide, seancorrales, batigolix, quicksketch. Log in to edit this page.

To send a confirmation email via additional processing you can use the script below.

It sends a confirmation email with data collected with the webform module

This assumes you have components in the webform called first_name, last_name, telephone and quantity

The names of the components are taken form the field "Field Key" in the fieldset "advanced settings" of the component

The whole php script including the php tags goes into the "additional processing" field in the fieldset "webform advanced setting" of the webform
Alternatively the script can placed in a php file on the server and called from the "additional processing" using the php include function

for Drupal 6

<?php
/**
 * this script send a confirmation email with data collected with the webform module
 * this assumes you have components in the webform called first_name, last_name, telephone and quantity
 * the names of the components are taken form the field "Field Key" in the fieldset "advanced settings" of the component
 * the whole php script including the php tags goes into the "additional processing" field in the fieldset "webform advanced setting" of the webform
* alternatively the script can placed in a php file on the server and called from the "additional processing" using the php include function
 */

// fetch data submitted via webform for later use in the body of the email
$params['first_name'] = $form_values['submitted_tree']['first_name'];
$params['last_name'] = $form_values['submitted_tree']['last_name'];
$params['telephone'] = $form_values['submitted_tree']['telephone'];
$params['quantity'] = $form_values['submitted_tree']['quantity'];

// fetch recipients email adrres from data submitted via webform
$to = $form_values['submitted_tree']['emailaddress'];

// set sender
$from = "luca.brasi@daluca.be";

// install devel module and uncomment next line to see entire data structure in a nifty little DHTML widget
// dpm($form);

// drupal message on confirmation page
drupal_set_message('Form has been sent');

// send the confirmation mail
drupal_mail('confirmation_reservation', 'confirmation_reservation_mail', $to, language_default(), $params, $from);

// construct email

function confirmation_reservation_mail($key, &$message, $params) {
 
$message['subject'] = "Confirmation of your reservation at Da Luca";
 
$message['body'] = "
Dear "
. $params['first_name'] . " " . $params['last_name'] . "
Thank you for reserving a table for "
. $params['quantity'] . " at our restaurant
In case you don't turn up we will now where to find you at"
. $params['telephone'] . "

Kindest regards,
Luca Brasi
Da Luca, Fish & Seafood Restaurant
"
;
    }
?>

for Drupal 5

<?php
$to
= $form_values['submitted_tree']['emailaddress'];
$first_name = $form_values['submitted_tree']['first_name'];
$last_name = $form_values['submitted_tree']['last_name'];
$telephone = $form_values['submitted_tree']['telephone'];
$qty = $form_values['submitted_tree']['quantity'];
$body = "Dear " . $first_name . $last_name . " \n\n
Thank you for reserving a table for "
. $qty . " at our restaurant \n
In case you don't turn up we will call you at"
. $telephone . " \n\n

Kindest regards,\n
Luca Brasi \n
Da Luca, Fish & Seafood Restaurant\n
Bladibla\n
Bladibla\n
Bladibla"
;

$mailkey = "reservation";
$subject = "Reservation from" . $first_name . $last_name . ;
$from = "luca.brasi@daluca.be";

drupal_mail($mailkey, $to, $subject, $body, $from, $headers = array())
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Yura Filimonov’s picture

What would be the code to use for Drupal 6? It'd be highly appreciated, thanks.

gtothab’s picture

Yes I need this code for the 6.x versions.

Thanks in advance!

grendzy’s picture

Here's the code I used. The drupal_mail() function has changed significantly in 6. I also used the webform's confirmation message for the email body.

<?php
$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 assumes there is a component on your webform called "email", that asks for the visitor's email address. If your component is named something else, you should change $to = $form['submitted']['email']['#value']; accordingly.

Also, you may wish to change the email subject.

PipB’s picture

I put this code in de additional processing field of my Webform from the Webform Modules and my email-field is the same.
But i didn't get any confirmation message.
I read somewhere that Webform ís not working with array's but strings and so you can't use these sign [] for the fields.
Is you're code really working?

clata’s picture

I'm having a similar issue, I need to get the value a user type and subsequentely submit from a textfield and I still do not have clear where to get it! I tried $form[submitted][name_of_the_field][#value] and many others but no luck for me! the bwst I could get was the output "Array". Does anybody know how to get the info I'm looking for and how to display it?
ciao.

grendzy’s picture

The code above is tested and working for me. I'm using Webform 6.x-2.3. It's possible the form structure changed in 2.4, I haven't looked.

One really easy way to find out what array keys you have is to install the devel module.

Then put dpm($form) in your code. dpm() will display the entire data structure in a nifty little DHTML widget.

yumminy’s picture

Thanks for the dpm() tip, grendzy. I was getting an empty array when I tried to print out the form structure on my confirmation page, but dpm($form) worked great.

Using Webform 6.x-2.3 and the email code above works for me too.

Fr0s7’s picture

There are several fields that accept PHP on the Webform configuration form. Among them are the 'Confirmation message or redirect URL' field, the redirect page itself, the 'Additional Validation' field, and 'Additional Processing' field. Where exactly do you put the call to the dpm($form) function?

fonant’s picture

$to = $form['submitted']['email']['#value'];

might be more robustly done as:

$to = $form_values['submitted_tree']['email'];

which I think is more the "Drupal way".

http://www.fonant.com - Fonant Ltd - Quality websites

peter panes’s picture

Put this in the Additional Processing field:

<?php

$to

= $form_values[submitted][8]; //replace 8 with the id of your email field
$subject = "Thanks";
$message = "Thanks for you enquiry blah blah blah.";

mail($to, $subject, $message, "From: website@ccn.ac.uk");

?>
BDS’s picture

Hi, How would you disable the webform auto email from the submitter? Not all of my forms require an auto email responder. Thanks much!

Elijah Lynn’s picture

You would simply uncheck all the items underneath "conditional email recipients".

-----------------------------------------------
The Future is Open!

buckley’s picture

Also, be sure that the user who is submitting has the rights to use PHP on the permissions page

adewinne’s picture

I had to rename:

function confirmation_reservation($key, &$message, $params)

to

function confirmation_reservation_mail($key, &$message, $params)

Before it would work

benjaminlhaas’s picture

I also had to rename this function.

drupal_mail takes "module" and "key" as its first two parameters, and then requires a "module_mail" function to do the sending. Since we're not using a module to provide this mail function, we have to explicitly provide it here. If we provide "confirmation_reservation" as the first argument to drupal_mail, then the function we have to provide is confirmation_reservation_mail.

To make things less confusing, we should probably also provide a different "key" to drupal_mail. The actual value of this "key" doesn't matter since we're not checking it in our mail function. Something like 'submitted' might work and not be confusing.

So the call to drupal_mail might look like:

<?php
// send the confirmation mail
drupal_mail('confirmation_reservation', 'submitted', $to, language_default(), $params, $from);
?>

And the signature of the mail function might look like:

<?php
function confirmation_reservation_mail($key, &$message, $params)
?>
opticalfuel’s picture

Hi, not a nubie to PHP, but rusty on Drupal...

Running D5.2x and I wonder if someone can spot me on this... I am looking for the place on my form.... that I am tasked to edit (add confirmation coding)

"The whole php script including the php tags goes into the "additional processing" field in the fieldset "webform advanced setting" of the webform
Alternatively the script can placed in a php file on the server and called from the "additional processing" using the php include function"

I don't see the additional processing field... is this unique to the D6.x version?

Thanks.

See this URL for screenshot of what I am seeing: http://www.caled.org/economicdevelopment/Webform_advanced_settings.png

benjaminlhaas’s picture

There should be two textfields right after "Submit button text" called "Additional validation" and "Additional processing". These are textfields where you're allowed to input PHP. If you don't see them, most likely you don't have the "use PHP for additional processing" permission. Go to /admin/user/access (if you have permission to view this page, of course), and make sure that "use PHP for additional processing" is checked for your role.

opticalfuel’s picture

Hi, not a nubie to PHP, but rusty on Drupal...

Running D5.2x and I wonder if someone can spot me on this... I am looking for the place on my form.... that I am tasked to edit (add confirmation coding)

"The whole php script including the php tags goes into the "additional processing" field in the fieldset "webform advanced setting" of the webform
Alternatively the script can placed in a php file on the server and called from the "additional processing" using the php include function"

I don't see the additional processing field... is this unique to the D6.x version?

Thanks.

See this URL for screenshot of what I am seeing: http://www.caled.org/economicdevelopment/Webform_advanced_settings.png

ANY IDEAS?

soulston’s picture

Make sure you have the php filter module turned on (admin/build/modules)

AlexanderPop’s picture

I used CCK field for email body text.

<?php
$to
= $form_values['submitted_tree']['email_address'];
$from = "info@sitename.com";
$body = $node->field_autorespond[0]['value'];

$message = drupal_mail('webform_extra', 'reply', $to, language_default(), array('body' => $body), $from, TRUE);

function

webform_extra_mail($key, &$message, $params) {
 
$message['subject'] = "Some text to see in subject";
 
$message['body'] = $params['body'];
}
?>

so email_address - component email item
field_autorespond - text CCK field (choose "hidden" in teaser and full node view in "display field" when edit content type )

wolfmarter’s picture

This is the error i get when i try and use the devel module:

warning: join() [function.join]: Invalid arguments passed in /is/htdocs/wp10475992_3K0QOVXJ1X/www/sites/all/modules/devel/krumo/class.krumo.php on line 657.

i have been trying for hours to get it to work.... I don know what the problem is. i even tried it with this simple script and it gave me the same error:

<?php
$to
= $form['submitted']['e_mail']['#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'];
}
?>

can anyone help me`?

pinkzilu’s picture

We allow users to upload attachments while filling out the form. And, we want to show the URL of attachments in the confirmation email. What is the code for that?

We used the "regular" code:

"One Attachment: " . $params['one_attachment'] . "\r\n" .

but got something like this:

One Attachment:
a:10:{s:8:"filename";s:12:"ismaili1.pdf";s:8:"filepath";s:46:"sites/default/files/webform/psa/ismaili1_1.pdf";s:8:"filemime";s:15:"application/pdf";s:6:"source";s:14:"one_attachment";s:11:"destination";s:46:"sites/default/files/webform/psa/ismaili1_1.pdf";s:8:"filesize";i:3969078;s:3:"uid";s:1:"1";s:6:"status";i:1;s:9:"timestamp";i:1283286276;s:3:"fid";s:4:"3040";}

Any suggestion?

Thanks!

thehandyman’s picture

Does anyone know how to do this with webform 3.x and webform php modules? It used to be accessible in 2.x, but in 3.x the variable is not set. It does appear ?sid=X on the confirmation page though.

ja09’s picture

Hello,

I'm emailing a copy of a submitted form with lots of fields.

The 6.x snippet on this page works great for me with webform 6.x-3.4.. but isn't there an easier way to write all fields to the email without $params for every single field.. I can't seem to make it happen.

.. something like

<?php
 

// fetch data submitted via webform for later use in the body of the email
$params['all_fields'] = $form_values['values']['submitted'];

// construct email
function confirmation_reservation_mail($key, &$message, $params) {
 
$message['subject'] = "Confirmation of registration";
 
$message['body'] = "

Thank you for your registration. Below is a copy for your records.

"
. $params['all_fields'] . "

Kindest regards,
Our Staff
"
;
    }
?>

Thanks for your help.

capono’s picture

Thanks for the documentation.

One more option to use: based on the choices made by the submitter (checkbox, radio) you can change the message body.

gcorbellini’s picture

hi all, thanks to your disccussion I could solve my problem. Just for info I use drupal 6.14 and this is my script:

<?php

$params

['first_name'] = $form['submitted']['first_name']['#value'];
$params['last_name'] = $form['submitted']['last_name']['#value'];
$params['telephone'] = $form['submitted']['telephone']['#value'];
$params['quantity'] = $form['submitted']['quantity']['#value'];

$to = $form['submitted']['email']['#value'];

// set sender
// if you want to put generic aitap address uncomment following
$from = variable_get('site_mail', '');
//else specify the sender
//$from = "abc@gmail.com";

drupal_mail('webform_extra', 'reply', $to, language_default(), $params, $from);

// drupal message on confirmation page
drupal_set_message('Form has been sent');

function

webform_extra_mail($key, &$message, $params) {
 
$message['subject'] = "Thanks for your joining this activity!.";
 
$message['body'] = "
This is an automatic confirmation email from the staff. Please do not answer to this email.

Dear  "

. $params['first_name'] . " " . $params['last_name'] . ", have received your registration request : you are almost done!
In order to complete your registration bla bla

Just to recall your choices, you said that your telephone number is "

. $params['telephone'] . " and that you desire to have " . $params['quantity'] . " tons of chocolate (gourmand!). If these information are not correct, please fill the form another time or contact the organizer.

Kindest regards,
the staff
"

;
}

?>

giorgio

hashie5’s picture

thanks the code for drupal 6 is working but how can I add html to the message?

LuisFernando’s picture

Hello everyone!
How about sending webform confirmation emails on Drupal 7 when an anonymous user completes the form?
I haven't found any info related to this anywhere on the web. Any help is appreciated.
Thanks!

SarangPSL’s picture

All of the above suggested methods for retrieving data from webform submission failed for me (for 6.x.3* version of webform). Suggesting a workaround which is tested for D6.22 + Webform 6.x.3* version:

1. Changed the Redirection location from Custom URL (which was not working) to Confirmation Page.
2. Set Input filter as php
3. Entered the below piece of code in the Confirmation Message text box:

<?php
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$nid = arg(1); // need to hard-code nid if this is a custom page
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
// if someone else is trying to snoop on the submission from a different IP, give them a 404
if ($submission->remote_addr != ip_address()) {
 
drupal_not_found();
 
module_invoke_all('exit');
  exit;
}
//Fetch the values of webform components as below
$name = $submission->data[1]['value'][0];//I had data[1] as a textfield component
$email = $submission->data[2]['value'][0];//I had data[2] as an email component
?>

<p>
Thank you <?php print $name?>, for submitting the form to us.<br>
We have sent an email on <?php print $email?> for your reference.
</P>

It worked. Let me know if anyone tried the above and faced issue.