Hey,

I need to have my webform's send confirmation emails to the users who submit them. I found this post which discusses the process for 5.x. I need to find out how to create this functionality for my 6.5 drupal version.

Does anybody have a snippet to place in the webform's "additional processing" field that will fire off an email to the user upon form submission????

Thanks!

Comments

Alan D.’s picture

Using the Rules module, you may be able to do this without any programming.


Alan Davison
www.caignwebs.com.au

Alan Davison
gtothab’s picture

Thanks for your reply. I'll look into that for shizzle.

Does anybody have any other suggestions??

Thanks again!

gtothab’s picture

It looks like webform is not on the list of modules that are compatible with rules.

CAN ANYBODY PLEASE HELP ME!!

Thanks

glennr’s picture

It depends on what you want in the email. If it's a basic confirmation email without customised fields, Rules could still work. That list at http://groups.drupal.org/node/10270/rules-modules is a list of modules with Rules integration. There's a big difference between integration and compatibility. Why not give it a try? There's even some documentation: http://drupal.org/node/298476.

Alternatively, have you tried submitting a Webform issue to see if that code at http://drupal.org/node/323666 needs tweaking for 6.x? The Webform developer is much more likely to keeping an eye on the Webform issue queue than the general post-installation forum.

gtothab’s picture

Hey,

Thanks for such an awesome post! Very Helpful. Well, I looked into creating a "triggered rule" using the module's selection of pre-set parameters but I dont have any way of referencing the user who submits the form's email in order to be able to have the rules module fire off an email to them. I'm assuming that when you said "give it a shot" you were referring to me creating some PHP that would be able to reference the user's email collected in the webform but I'm afraid I dont have the PHP chops to make that happen :(

Your advice for submitting an issue for the webform module in order to have a 6.x snippet made available for confirmation emails sounds very apealing. I'm definately gonna look into this.

If anybody has any suggestions, they'd be very much appreciated .

Thanks again to glenr

Peace,

Graphbyte

glennr’s picture

Rules may possibly have a no-coding solution. Sorry, I don't have Rules currently installed on my dev site, but I seem to remember it has some options like this:

Event: Something like "saving new content"
Condition: There should be an option to select the Content Type (ie Webform)
Action: I think there's an option to send mail to the author

Would this work? I've never done anything like this, so you'd need to test it of course, and you probably won't be able to include custom fields such as the user's name in the email.

Let us know how you go.

But definitely try the Webform issue queue too. There's no guarantee of a solution, but the developer will at least see your problem.

Alan D.’s picture

The only significant change is to drupal mail

<?php
$to = $form_values['submitted_tree']['email'];
$extra_params['message'] = "Dear asdf \n\n
Thank you for reserving a table for at our restaurant \n
In case you don't turn up we will call you at \n\n

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

$mailkey = "reservation";
$from = "alan@example.com";
$extra_params['subject'] = "Reservation from";
$extra_params['from'] = $from;

drupal_mail('none', $mailkey, $to, language_default(), $extra_params, $from);

?>

I'm getting a spam filter triggered, (the ISP's fault, my mobile broadband IP is in their blacklist), so it is not fully tested.

Rules integration will take a fair amount of work to integrate, maybe a day or two, so this is a much easier solution.


Alan Davison
www.caignwebs.com.au

Alan Davison
gtothab’s picture

Thanks Alan,

This fires off an email but it comes out blank. Also, the subject is left blank as well. I'm guessing something is not right with the "extra_params" array but I dont know how to fix it.

Any ideas???

thanks again that was awesome.

Fernando

Alan D.’s picture

I have no way to test, but the first guess I have would be to change $extra_params['message'] to $extra_params['body']. This is how other calls to drupal_mail seem to be doing it in the source code.

Let me know if it works, and I'll update the other page where the example came from.

Cheers


Alan Davison
www.caignwebs.com.au

Alan Davison
gtothab’s picture

Hey,

Thanks again. Umm so I tried changing $extra_params['message'] to $extra_params['body'] and that sent out an empty email too. Then I tried changing it to just $body = "My message"; and then including a call to that variable in the drupal_mail() function and that failed to send the email altogether.

So Im out of ideas! If you got any, I'd love the help.

Thanks,

Graphbyte

Alan D.’s picture

The function drupal_mail relies in a hook_mail_alter or module_mail for the email body to be set. A quick look into the core hook_mails didn't expose a simple module to choose from, so I've hacked together the following:

<?php

$to = $form_values['submitted_tree']['email'];
$params['body'] = "Dear asdf \n\n
Thank you for reserving a table for at our restaurant \n
In case you don't turn up we will call you at \n\n

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

$key = "reservation";
$from = variable_get('site_mail', ini_get('sendmail_from'));
$params['subject'] = "Reservation from";
$params['from'] = $from;
$send = TRUE;
$language =  language_default();
$module= 'none';

  $default_from = 

  // Bundle up the variables into a structured array for altering.
  $message = array(
    'id'       => $module .'_'. $key,
    'to'       => $to,
    'from'     => $from,
    'language' => $language,
    'params'   => $params,
    'subject'  => '',
    'body'     => $params['body']
  );

  // Build the default headers
  $headers = array(
    'MIME-Version'              => '1.0',
    'Content-Type'              => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer'                  => 'Drupal'
  );
  $headers['From'] = $headers['Reply-To'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $from;
  $message['headers'] = $headers;

  // Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail.
  drupal_alter('mail', $message);

  // Concatenate and wrap the e-mail body.
  $message['body'] = is_array($message['body']) ? drupal_wrap_mail(implode("\n\n", $message['body'])) : drupal_wrap_mail($message['body']);

  $message['result'] = drupal_mail_send($message);

  // Log errors
  if (!$message['result']) {
    watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR);
    drupal_set_message(t('Unable to send e-mail. Please contact the site admin, if the problem persists.'), 'error');
  }

?>

The must be an easier way to do this, but it works. I can do tests from work, and they came through with the body intact. :)

If you use one of the HTML mail modules, then there will be a cleaner way of doing this.

Best of luck

Alan


Alan Davison
www.caignwebs.com.au

Alan Davison
gtothab’s picture

Hey,

Thanks for that a lot. I pasted that whole snippet into the "additional processing" field in the global settings for the webform but that made the confirmation email not be sent altogether.

I was wondering if you meant for the whole snippet to be used or if part of it was meant to be placed inside of template.php in order to override some function.

Let me know if that's the case and I'll implement it for sure and thanks again for your time; its much appreciated.

Fernando

Alan D.’s picture

The test that I ran was the above code that was in a webform that had one field 'email'.

Double check the site email, and the web form parameters.

The line $from = variable_get('site_mail', ini_get('sendmail_from')); can be replaced with $from = 'your@email.address';


Alan Davison
www.caignwebs.com.au

Alan Davison
gtothab’s picture

Hey,

I dont know why but this is not working. I checked and the site's email and the email in the webform settings and its the same one. Then I tested making the from variable be the string of my choosing instead of it referencing the site's email and that didnt work either.

The webform itself has some textfields, a select, an email and a textarea. The webform is sent to the intended administrator but the confirmation email is not sent to the user :(

Now, just to confirm, that snippet you included before with all of those array declarations is meant to be included in the webform's "additional processing" field right? That part (im speaking of everything below $default_from = . . . ... . ) isnt meant to be in template.php is it?

Big big thanks again to you. This site is up and running and the client told me that he needed this functionality and I was caught somewhat off guard. Your help is wonderful.

Sincerely,

Fernando

Alan D.’s picture

Can you paste your code as an example? I'll have a quick look at it. And a link to the site to allow me to mimic the form.

Cheers


Alan Davison
www.caignwebs.com.au

Alan Davison
gtothab’s picture

Hey it worked! I had forgotten to change the form values input keys to the ones with the wording used in my actual webform (i.e. $to = $form_values['submitted_tree']['correo_electronico',]; instead of ['email']; I HAD done this with your previous snippets but had forgotten to do so for this last one that you posted.

So listen, thanks so much for your help. I'dda been in a sticky situation without it. If you've got a paypal account on your site for handling donations or something, I'd love to buy you a beer.

Let me know,

Fernando

Alan D.’s picture

No worries. I'm glad that it worked.

If you ever need a partner to do some more in depth Drupal projects, feel free to get in contact us! About half our work is for designers implementing different Drupal solutions.

Cheers

Alan


Alan Davison
www.caignwebs.com.au

Alan Davison
gtothab’s picture

Ok sure, I'll bookmark the link in your signature. I have a project that has some requirements that im real fuzzy about at this point. If I dont clear most of those doubts up before the start date, I'll be sure to give you a ring.

Peace

plousia’s picture

I have a site where I needed to do this, and I used your code. Thanks so much!

bamest4christ@gmail.com’s picture

How do i make my contact page send a confirmation message to people after filling it on drupal 7

mediamash’s picture

is it possible to send HTML mails to with this solution?

rahulbile’s picture

BrightBold’s picture

Thanks, this solution was simple and worked great. I'm looking forward to the built-in Webform solution in the 6.x-3.x release (still in dev). Hopefully that will include more functionality - I'd like to be able to send a different reply depending on the contents of the form. But for now, this offers a solution to my basic problem.

BrightBold’s picture

Oops, I spoke too soon. Webform (at least on my site) strips the slashes out of \r\n in the code so you get the following error:
"Parse error: syntax error, unexpected T_STRING in /mysiteroot/sites/all/modules/webform/webform.module(1759) : eval()'d code on line 1"

Here's how to solve this: You have to double the \ into \\ in every instance (so "\\r\\n") before you save the form. The catch is: each time you save the form, one set of backslashes is stripped. So if you make any edits to the form, you will need to add a second backslash to each existing one before you save.

bombo’s picture

Hi, tried this on my site and it works perfectly.
Would be good to implement such functionality in a new Webform release. The need to address the subscriber with a different mail than that, which goes to the Website-Owner is obvious.

greetings from germany, bombo

teh_catt’s picture

got me out of a big spot as I couldnt remove a field because there is no delete option in webform but managed to turn it into an email confimation field instead!