Custom Email Templates for Webform

The Webform module allows you to send different emails to different people but what about changing the structure of your email depending on what the user enters in the form?

This functionality comes in handy for Job Application forms where you might want to change the Email the user receives based on the available position they are applying for.

The way to do this is to override Webform's mail template with your own custom template file, webform-mail-[node-id].tpl.php, which contains the node ID of the webform you want to change. Within this template file you can then create an if statement to check the user's input and change the content of the email accordingly. In the below example I have changed the text within the email based on whether the user has applied for a general position or a specific job.

<?php
  if ($submission->data[1]['value'][0] == 'General Application'){
    print 'Thank you for submitting your resumé and covering letter'."\n\n";
  } else {
    print 'Thank you for submitting your resumé and covering letter for the position of '.$submission->data[1]['value'][0]'."\n\n";
  }
?>

Once you have created your own Webform template file you then have to set the email to use your custom template in place of the Default. This is done by changing the Email Template option on the Edit Email Settings page to be Custom template.

NOTE: If you are sending multiple emails within the same Webform and you only want to make structural changes to one of those emails, you will have to add a little extra into your template. In order to specify which email template you want to change you simply need to check the $email variable for the correct email ID before applying any changes. The email ID is visible in the URL on the Edit Email Settings page, i.e. http://website.com/node/2/webform/emails/1, the ID would be 1. In the below example I am changing the email structure only if the email ID is equal to 2.

<?php
  if ($email['eid'] == 2) {
    //Changed structure
  }
  else {
    //original structure
  }
?>
Posted by Teegan Blond
Teegan is our not-so-girly female developer. She is a self-proclaimed gamer and loves everything scary, including reading Stephen King and fighting Zombies on her Laptop.

Leave a reply

The content of this field is kept private and will not be shown publicly.