I have a webform (5.x-2.x) collecting form results and sending them to a single email address using a custom webform-mail-##.tpl.php template file with no problems; really impressed! Now, I know I can send that same email to different email addresses using the built-in 'Conditional e-mail recipients' option and not setting any conditions.

But, how would I preform the following scenario:

  1. Send one email, themed for a customer, to their own email address (a confirmational 'Thank You' email)
  2. Send another email, themed for internal business use, to another designated address.

A form I'm working on has an email field (named 'email') that would be used to send the customer email, and the other is the email address specified in the webform's mail settings that would be used to send the internal-use email. I'd like to send different emails, with very different content, to each address.

I figure this could be done either in the webform's additional processing (routing the email through two different email template files), or changing it in the template based on which address is being processed by Drupal. Since version 2 sends emails individually, is there a way I can determine which one is currently being processed in the webform-mail-##.tpl.php file?

Either way, I have no idea which route to take and how to do it.

Thank you for your help.

Comments

quicksketch’s picture

Fortunately the new 2.x mailing system is way more flexible than 1.x. You can do this easily enough by checking the last parameter ($cid) in your [themename]_webform_mail_message() function. This contains the component ID of the conditional email recipient that will receive the current email being themed. This makes it so that you can theme separate emails for every recipient of your webform.

If you want to get really specific you could do this approach:

function phptemplate_webform_mail_message_[node id here]($form_values, $node, $sid, $cid) {
  if ($cid = 10) {
    $template = 'webform-mail-[node id here]-customer';
  }
  else {
    $template = 'webform-mail-[node id here]-administrator';
  }
  return _phptemplate_callback($template, array('form_values' => $form_values, 'node' => $node, 'sid' => $sid, 'cid' => $cid));
}

Or a simpler approach, you can simply use IF statements inside of your current webform-mail-nid.tpl.php file depending on the $cid.

pawpetcare’s picture

Thank you quicksketch! That did the trick, but I had to change two things in your code for it to work correctly.

function phptemplate_webform_mail_message_57($form_data, $node, $sid, $cid) {
  /* Select appropriate email template */
  if ($cid == 3) {
    /* $cid #3 is the prospect's email address field */
    $template = 'webform-mail-request-information-prospect';
  }
  else {
    $template = 'webform-mail-request-information-company';
  }
  return _phptemplate_callback($template, array('form_data' => $form_data, 'node' => $node, 'sid' => $sid, 'cid' => $cid));
}

First, I needed to use two equal signs, not one.
Second, I had to use $form_data instead of $form_values. I got an error when the email theme was processing a loop to display multiple-select checkbox items. Using $form_data didn't have this problem. What's the difference between the two?

After these two minor changes I was able to send differently themed emails to each recipient. Very nice!

Now, one last question. Is it possible to change the subject, the 'from' email name, and the 'from' email address for each email as well? Expanding upon the different email body theme, the customer's email subject should be something like 'Thank you for visiting our website' and the company's email subject would be something appropriate for their use. As it stands now, both emails have the same subject, etc.

Thanks again.

quicksketch’s picture

You can change any of the set headers by theming theme_webform_mail_headers() or theme_webform_mail_headers_57() for your particular webform node. Just copy the code out of webform.module and change as necessary in your template.php file.

Thanks for the corrections in my code :)

pawpetcare’s picture

Thanks again quicksketch. I've got the subject and from email address now changing as I like using the following code.

function phptemplate_webform_mail_headers_57($form_values, $node, $sid, $cid) {
  /* Required email header */
  $headers = array(
      'X-Mailer' => 'Drupal Webform (PHP/'. phpversion() .')',
      );
  /* Select appropriate email header */
  if ($cid == 3) {
    /* $cid #3 is the Prospective Client's email address field */
    $headers['Subject'] = 'Thank You';
    $headers['From'] = 'noreply@inhomepetcare.com';
  }
  return $headers;
}

I have one small problem though. If the webform's 'Email from address' is set to 'secret.unpublished.email@companydomain.com', the customer would receive an email addressed as:

secret.unpublished.email@companydomain.com on behalf of noreply@companydomain.com

Obviously, I would like the 'secret unpublished email address' to stay just that way, unpublished. Do you know how this can be corrected so the address just comes through as 'noreply@companydomain.com'?

quicksketch’s picture

drupal_mail() sets some additional headers that you might also try setting:

$headers['From'] = $headers['Reply-To'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $from
pawpetcare’s picture

That was just what I needed quicksketch!

Here was my final code for the custom mail header for anyone that needs it:

function phptemplate_webform_mail_headers_57($form_values, $node, $sid, $cid) {
  /* Required email header */
  $headers = array(
      'X-Mailer' => 'Drupal Webform (PHP/'. phpversion() .')',
      );
  /* Select appropriate email header */
  if ($cid == 3) {
    /* $cid #3 is the Prospective Client's email address field */
    $headers['Subject'] = 'Thank You';
    $headers['From'] = '"'. mime_header_encode('Company Name, Inc.') .'" <'. 'email@companydomain.com' .'>';
    $headers['Sender'] = $headers['Reply-To'] = $headers['Return-Path'] = $headers['Errors-To'] = 'email@companydomain.com';
  }
  return $headers;
}

Quicksketch, thank you very much for all of your help.

quicksketch’s picture

Woot! You're welcome :)

Rafał Ch’s picture

Thank you for your help and advice. It really works! :-)
And I have a question:

I use select component populated from taxonomy (thanks to http://drupal.org/node/162948) to send conditional e-mails. But vocabulary contains names, not e-mails. I thought I'd change $headers['To'] for appropriate cid in phptemplate_webform_mail_headers_xx() but it seems I only add another recipient...

How to unset the first malformed recipient containing text from the selection?

marcvangend’s picture

Pawpetcare and quicksketch,
Thank you for the excellent code you put together here. I think it deserves to be added to the webform documentation on http://drupal.org/handbook/modules/webform.

quicksketch’s picture

@marcvangend, please add a handbook page to document this information. Just click "Add child page" at the bottom of the main webform docs page.

@tzolkin, open a new request for your issue.

quicksketch’s picture

Status: Active » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

manoloka’s picture

Wow, thanks a lot (pawpetcare and quicksketch) this is exactly what I needed :-)

I've managed to send 2 different emails with different headers nearly successfully.

I only found 1 small problem;

The secondary subject (i.e Thank You) appears as well as the default subject,

e.g. My default subject Thank You

Any idea why ? or how to correct it?

manoloka’s picture

... uhm ... that's weird

Depending in which email account/software I read the message I get different formatting.

Some show the old title + the one modified with the Headers code, some show only the modified ... any suggestions?

mariagwyn’s picture

Status: Closed (fixed) » Active

Question:

@ comment #2: what are the templates to which you are referring in this function? do you have a sample, and where do you put it (template.php, webform-mail.tpl), etc.

@ comment #1: how do you use IF statements in teh webform.tpl file? Simple example please?

@comment #6: Can I add form fields in one of these lines. For instance,

$headers['Sender'] = $headers['Reply-To'] = $headers['Return-Path'] = $headers['Errors-To'] = 'USER ENTERED EMAIL FORM FIELD';

BTW, I am on 6.x

Thanks,
Maria

quicksketch’s picture

@mariagwyn, read the THEMING.txt file included in the Webform download for an introduction to theming Webform. The *.tpl.php files are all included in the webform module directory. You can simply copy them to your theme directory and then edit them as necessary (after you clear the Drupal cache, again, refer to THEMING.txt).

As for IF statements, I'd recommend looking at an introduction to PHP or purchasing a book on the topic.

mariagwyn’s picture

I have read the THEMEING.txt file, downloaded the tpl files, etc. But I cannot find any example of how to call particular fields in the file. I can UNSET fields, but I can't seem to figure out how to do anything beyond calling ALL the fields and unsetting a few. This means that I can't theme the entire form or email b/c I am just not sure what the syntax for a single call is. Do you (or anyone) have an example of a printing a single field?

For example, how to I modify the following code to print ONLY the name field rather than print everything but the name field, without 'unsetting' everthing?

<?php
  // Print out all the Webform fields. This is purposely a theme function call
  // so that you may remove items from the submitted tree if you so choose.
   unset($form_values['submitted_tree']['name']);
  print theme('webform_mail_fields', 0, $form_values['submitted_tree'], $node);
?>

In other words, I want 'name' to appear in some html, I want subject to appear in another area of the email.... I have tried a variety of bits, but just don't get the php in this well enough to know how to call my list of form values (and yes, I printed all the values so I know what my options are).

Thanks,
Maria

quicksketch’s picture

If you need to print out a single field, you can use the theme_webform_mail_fields() function for that particular field.

  $cid = 1; // You'll need to map this component ID.
  print theme('webform_mail_fields', $cid, $form_values['submitted_tree']['name'], $node);

  $cid = 3;
  print theme('webform_mail_fields', $cid, $form_values['submitted_tree']['email'], $node);

This isn't ideal I know, as there should be theme functions (or .tpl.php files) for individual fields also, but there aren't specific field templates yet.

mariagwyn’s picture

Thanks, this will get me started. Do you know of any theme that already has custom mail.tpl files? I will try to work up a 'how-to' on this once I have time.

m

dhaws’s picture

Does this work in 6.x also? If so, how would I go about implementing?

I got this to work on the headers in 6.x by adding the overriding function [theme name]_webform_mail_headers($form_values, $node, $sid, $cid) in the template.php file, but how do I change the mail body?

budda’s picture

I've got an auto response module ready for 5.x usage, see http://drupal.org/node/327153

For the email headers issue, try the http://drupal.org/project/returnpath module too.

quicksketch’s picture

Status: Active » Closed (fixed)

dhaws, it's all explained in THEMING.txt. I'm closing this issue again.

codemann’s picture

I just tried this, but for some reason it won't work properly. I added this to my template.php :

function phptemplate_webform_mail_message_10($form_data, $node, $sid, $cid) {
  /* Select appropriate email template */
  if ($cid == 5) {
    /* $cid #5 is the customers email address field */
    $template = 'webform-mail-customer';
  }
  else {
    $template = 'webform-mail';
  }
  return _phptemplate_callback($template, array('form_data' => $form_data, 'node' => $node, 'sid' => $sid, 'cid' => $cid));
}

function phptemplate_webform_mail_headers_10($form_values, $node, $sid, $cid) {
  /* Required email header */
  $headers = array(
      'X-Mailer' => 'Drupal Webform (PHP/'. phpversion() .')',
      );
  /* Select appropriate email header */
  if ($cid == 5) {
    /* $cid #5 is the customers email address field */
    $headers['Subject'] = 'Thank you';
    $headers['From'] = '"'. mime_header_encode('Companyname') .'" <'. 'info@companyname.be' .'>';
    $headers['Sender'] = $headers['Reply-To'] = $headers['Return-Path'] = $headers['Errors-To'] = 'info@companyname.be';
  }
  return $headers;
}

I'm using the zen theme, so to be sure he would use the template I added webform-mail.tpl.php and the customized webform-mail-customer.tpl.php to the general zen folder and my subtheme folder.

The weird thing is that he does the headers function, but he still uses the default template. I also turned on the reloading of the theme on every page to be sure that caching can't be the problem.

I'm using Drupal 6.8 and Webform 6.x-2.3. Am I overlooking something?

donquixote’s picture

Category: support » feature
Status: Closed (fixed) » Active

Hey, I think this use case is quite common and obvious, don't you think it should be a feature?

I understand this can be done on the theme layer, but
- I think this is far too common to require a theming solution.
- Theme solutions are usually a bad idea on sites which allow more than one theme, or where you want to switch themes in the future. (You could do this with a custom module instead, using hook_theme_registry_alter, if you are comfortable with that, so ok, this argument might be invalid)
- It is never nice having to juggle with node ids ($nid) and field ids ($cid) in code. For instance, it can easily happen that auto increment ids (that is both $nid and $cid) on a test site do not match with those on a live site. Which means, you would have to change the code when you deploy it, or go an extra mile trying to avoid the hardcoded ids.
- I generally don't like heavy interdependencies of configuration (living in the database) and code (theme and module). For instance, if you want to replace the email field with a dropdown (for whatever reason), you have to change the code as well.

I also had the idea that it would be nice if email text could be configured with a textarea + tokens, as an alternative to the theme function.

I'm not sure if all of this needs to be in webform core. I could also imagine this stuff to live in a separate module. Depends what you say.

quicksketch’s picture

Category: feature » support
Status: Active » Fixed

Hey, I think this use case is quite common and obvious, don't you think it should be a feature?

You can already send per-user emails in the 3.x version, and the templating is all built into the interface so you don't have to use code at all (basically all of your suggestions). See #277581: Send emails with a custom/templated body (and subject, to addresses and from addresses).

Please open a new request for any additional features you would like added to the existing functionality.

Move this back to a fixed support request.

donquixote’s picture

Nice to hear :)
I hope that 3.0 will be ready soon!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

woodsman’s picture

Version: 5.x-2.x-dev » 7.x-3.18
Status: Closed (fixed) » Active

Is sending differently themed emails to different email addresses based on $cid still supported using the methods described in this thread? I can't seem to get it to work using the latest version of webform. With my theme named Orange, I have the following function in my template.php file:

function orange_webform_mail_message_685($form_data, $node, $sid, $cid) {
  /* Select appropriate email template */
  if ($cid == 1) {
    /* $cid #1 is the user email address */
    $template = 'webform-mail-685-user';
  } else {
    $template = 'webform-mail-685-company';
  }
  return _orange_callback($template, array('form_data' => $form_data, 'node' => $node, 'sid' => $sid, 'cid' => $cid));
}

and I have these files in my theme directory:

/template.php
/templates/webform-mail-685-user.tpl.php
/templates/webform-mail-685-company.tpl.php

The "user" template should go to the user submitting the form, and the "company" template is sent to the HR department. But the emails always use the same default webform theme and never use the template files. I'm not sure what to do or how to debug this. :( Any help would be greatly appreciated. (Also please forgive me if I shouldn't have changed the Status.)

Edit: Well crap, it looks like I didn't select the correct version of Drupal in the meta options for this post. I'm using the latest release of Drupal 7 and Webforms 3.

quicksketch’s picture

Version: 7.x-3.18 » 5.x-2.x-dev
Status: Active » Closed (fixed)

Is sending differently themed emails to different email addresses based on $cid still supported using the methods described in this thread?

Yes, it's quite a bit different now. Read the THEMING.txt file that comes with Webform. The instructions posted here were for Drupal 5. However now you just set up two e-mail configurations and use the built-in template tool. You don't really need to use any PHP templates at all (through you still can if you want to).