JavaScript Pre-Processing Examples

Available Script Context

message.getBody() is not available within the JavaScript Script Pre-Processing Task. Only header values can be extracted/set.

API

Notes

API

Notes

message.getAllHeaders()

Get all headers of a message.

message.getHeader(name)

Get value of a message header

Parameters

  • name the name of the header to get

message.setHeader(key, value)

Set the value of a message header

Parameters

  • key (String) the name of the header to set

  • value (String) the value of the header to set

print(str)

Adds output that is recorded during execution and shown in the processing report.

Parameters

  • str (String) the string to be recorded

setOutcome(outcome, reason)

Set the processing outcome of the script

Parameters

  • outcome (String) the outcome type available outcome values: DROP, IGNORE, FORWARD

  • reason (String) the reason for the outcome

assert

See below. https://nodejs.org/api/assert.html

RegExp

Regular expressions can be evaluated within the script. This is often used for searches within string values for matching content. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp for more information.

Assertions

Textual Assertions

See https://nodejs.org/api/assert.html for full Assert API documentation.

assert.equal('The actual value', 'An expected value', 'Helpful message');

Where the first value is an expression which captures a value in the email after it has been processed by your script, the second value is an expression indicating the value you expect to receive, and the third value is any message you wish to set. This final value will be output when an assertion fails, so it is recommended to set the message to something you will find useful.

Running the above Test Script example against any email will produce the following output (if you do not with to provide a message you can simply omit it)

assert.equal('The actual value', 'An expected value');

A more realistic example is as follows.

Example Email:

MIME-Version: 1.0 Received: by 10.223.112.12 with HTTP; Sat, 18 Jun 2011 22:42:26 -0700 (PDT) Date: Sun, 19 Jun 2011 17:42:26 +1200 Subject: Some other subject From: andy@thepluginpeople.com To: test@example.com Content-Type: text/plain; charset=UTF-8 CHANGE BODY!!!

Test Script:

Here we assert that the Subject has a certain value.