1

I have a webform and I want to send email on submission to this form.

Currently, I configured the emails and its working fine. However, I would like to put conditions on sending the email. Like if field_1 = "xyz" then change the email content to "abcd" else change email content to "pqrs"

Can this be done using the inbuilt module for emails? Please help

3 Answers 3

11

I reckon you can get there just using Webform and applying some Conditions. Here is the recipe I just tested.

  1. Add a 'country' select field, with options NZ and USA
  2. Add a 'usa1' textfield, set as private, and enter default value you want to go in your email when USA is chosen ('love usa').
  3. Ditto for an NZ textfield (nz1) ('go nz').
  4. Now set up a condition that will 'show' (to admins only as it is private) the USA text field when USA is chosen as country,
  5. ditto a condition so if NZ is chosen, the NZ text fields 'shows'.
  6. Now set up your email with eg [submission:values:usa1][submission:values:nz1]

and presto hey!

If i select USA as country I get email saying 'love usa'. if i chose NZ I get 'go nz'

Is that pretty much what you need?

NB: I thought this route would work as we had done similar when we needed an email to go out to addressX 'only' if a user chose USA as country. We used webform conditions and We craeted an email field that was set to private. Then used conditions to 'show' it (to admin only) if USA was chosen. Then we could just set up a new webform email using that field, and it only fired if the condition was met.

1

I would probably use the Webform Rules module (https://www.drupal.org/project/webform_rules) and then create a rule that on submission of the form, it is sent to the appropriate role or a specific email address is that was required.

0

You can change the rendering of a webform submission easily. See hook_webform_submission_render_alter.

I wrote this code 2 days ago to change a submission embedded in a View, and also change the submission's appearance in a PDF from webform2pdf. The same approach is doable using Emails. I hate webform 4's new tokens. I bypass them completely.

/**
 * Implements hook_webform_submission_render_alter().
 *
 * Add a download PDF link to the beginning of the submission view.
 */
function MYMODULE_webform_submission_render_alter(&$renderable) {
  // use Devel to see what $renderable array looks like.
  // dpm($renderable);

  // If we're on a custom View path embedding a submission, add a download
  // link.
  if (current_path() == 'open-applications') {
    $nid = $renderable['#node']->nid;
    $sid = $renderable['#submission']->sid;
    $download_pdf_link = l('Download Pdf', "node/$nid/submission/$sid/downloadpdf");
    $renderable['reference_regarding']['#prefix'] = $download_pdf_link.'<br>';
  } else {
// DEBUG
// ... if we wanted to prefix the $renderable array with say a <style> tag
// we could do that via  #prefix. I couldnt get this to work for PDFs.
//    $renderable['reference_regarding']['#prefix'] = '**' . substr(current_path(),-11) . '**';
  }
  // This glorious hack from: https://drupal.org/node/1597892
  //
  // and see:
  // http://drupal.stackexchange.com/questions/50086/how-to-use-submissionvalues-in-email-template-in-webforms

  // If we're downloading a PDF, checking the page url, then append break tags
  // after a title.
  if (substr(current_path(),-11) === 'downloadpdf') {
    foreach ($renderable as $i => $item) {
      if (is_array($item) && !empty($item['#title'])) {
        $renderable[$i]['#title'] .= "<br>";
      }
    }
  }
  // Unset a hidden webform field from viewing, which I have pointing to a
  // Relation ID.
  unset($renderable['webform_rid']);
}

Like I said I bypass the use of tokens in my PDF with the use of a helper function and also custom PHP in the PDF content body (you could do the same for your email body):

/**
 * This function builds an array of HTML fragments of rendered submissions from
 * a collection of Submission IDs.
 */
function _MYMODULE_render_reference_submissions($sids) {
  $content = array();
  if (count($sids)) {
    module_load_include('inc', 'webform', 'includes/webform.submissions');
    $webform_nid = 1;
    $webform_node = node_load($webform_nid);

    foreach ($sids as $sid) {
      $submission = webform_get_submission($webform_nid, $sid, TRUE);
      $webform_renderable = webform_submission_render($webform_node, $submission, '[email protected]', 'html');
      $content[] = drupal_render($webform_renderable);
    }
  }
  return $content;
}

My PDF body content custom PHP code (requires Core PHP input being enabled) is as follows:

<?php
// path of a submission PDF download is of the format: 
// node/1/submission/1/downloadpdf
$path = current_path();
$parts = explode('/', $path);
$sid = $parts[3];
$content = _MYMODULE_render_reference_submissions(array($sid));
return $content[0];
?>

Webform submissions can be rendered as like plaintext, html and something else I think out of the box via the $format value passed into webform_submission_render.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.