Template Scripting

Check out how you can implement template scripting in your emails.

Script fields are denoted with {{ }}.

Make sure Template Scripting is enabled in your Account's Sending Settings.

Javascript can be used inside the script fields.

Script fields can be used in the subject, HTML, and text body.  **Note: due to subject field size limit, it's advised to put main scripting in the HTML and use short expressions in subject element.

The scripting engine also includes the Contact and Profile Merge Fields, for example {{ var myname = accountfirstname + ' ' + accountlastname; }}. Fields defined in CSV mail merge are also accessible by their column names. Note that the generic Merge Fields such as {{unsubscribe}} or {{view}} can not be used currently with Template Scripting.

Exceptions

A Javascript code error inside the script field will prevent the email from being sent and will be reported as a bounce. Standard JavaScript try/catch can be leveraged to control error handling.

Additional Functions

  • md5(text) - returns the MD5 hash of given string.
  • download(url) - downloads and returns page content as string. If remote server can't be contacted, an exception is thrown.
  • htmltag(html, tag) - returns value of a given tag, e.g. var subject = htmltag(content, 'title');

Example 1

Subject line

{{ subject }}

HTML

{{
var html = download('http://somewhere.com?recipient=' + encodeURIComponent(email)); // note: email is defined as a part of standard merge field set.
html = html.replace('[-EMAILADDR-]', email);
html = html.replace('04102014', Math.floor(Math.random() * 100000000));

var subject = htmltag(html, 'title');

htmltag(html, 'body'); // this line evaluates to content in the body tag and is returned by this script block
}}

Example 2

Subject line

Today is {{new Date()}}.

HTML

{{
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
null; // this line is so this script block evaluates to null, otherwise it would have evaluated to content of today variable.
}}