Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 20 Next »

Available Script Context

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

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

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

Textual Assertions

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.

assert.equal(message.getHeader('subject'), 'test subject', 'The email subject was not as expected');

In the example email, the Subject is not the expected one, and therefore the following output will be produced:

Script examples

Adding an email address to your emails

Script:

var to = message.getHeader('to');
var newTo = to + ', someone@example.com';
message.setHeader('to', newTo);

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: CHANGE_SUBJECT!!!
From: andy@thepluginpeople.com
To: test@example.com
Content-Type: text/plain; charset=UTF-8

CHANGE BODY!!!

Test Script:

You can assert the test case after it is processed by your script, by using the following assert. You should see that the email will have two “to” addresses, the original email address and the one the script adds.

assert.equal(message.getHeader('to'), 'test@example.com, someone@example.com', 'Example message');

Detecting a missing From address and adding it to the email

Script:

var from = message.getHeader('from'); //Extracts the Value found within the From header
if (from == null) { //Checks if From address is missing
    message.setHeader('from', 'newaddress@example.com'); //Sets the From address
}

Test Script:

The following assert will check that the From address is now set correctly.

assert.equal(message.getHeader('from'), 'newaddress@example.com', 'Example message');

Replacing the From address

This example shows how to manipulate the From address, to detect one domain and swap it out for another.

Script:

var original = message.getHeader('from').toLowerCase(); //Extracts the Value found within the From header
if (original.endsWith('@thepluginpeople.com')) { //Checks if sender address is a specific address
    var name = original.split('@')[0];
    message.setHeader('from',name + '@replaced.com'); //Changes original from address to be the extracted name and email address
}

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: CHANGE_SUBJECT!!!
From: ryan@thepluginpeople.com
To: test@example.com
Content-Type: text/plain; charset=UTF-8

CHANGE BODY!!!

Test Script:

assert.equal(message.getHeader('from'), 'ryan@replaced.com', 'Example message');

Processing Report:

Here you can see the example mail processing report generated by JEMHC for our support project. A user would have been associated if it didn’t exist, in this case we’ve simply created a new portal user.

Extracting an address from the name part and setting it as the From address

If you are receiving emails that are sent from a system address with a personal part that contains the actual sender address, the script below will extract the actual sender address from the original personal part and will set this value as the sender of the email, so that it can be processed correctly.

Example Script:

var original = message.getHeader('from').toString(); //Extracts the Value found within the From header
if (original.includes('system@external.com')) { //Checks if sender address is a specific address
    var address = original.substring(original.indexOf('(') + 1, original.indexOf(')')).trim(); //Extracts the address from between the two brackets "("")"
    var personal = original.substring(original.indexOf('"') + 1, original.indexOf('(')).trim(); //Extracts the real senders personal part (Name)
    message.setHeader("from", '"' + personal + '" ' + '<' + address + '>'); //Changes original from address to be the extracted name and email address
}

Example Test Case:

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: CHANGE_SUBJECT!!!
From: "ryan example (ryan@example.com)" <system@external.com>
To: test@test.com
Content-Type: text/plain; charset=UTF-8

CHANGE BODY!!!

Test Script:

assert.equal(message.getHeader("from"), '"ryan example" <ryan@example.com>', 'Example message');

Processing Report:

Here you can see the example mail processing report generated by JEMHC for this Test Case. A user would have been associated if it didn’t exist, in this case we’ve simply created a new portal user with the correct email address and name.

Using Regex to extract a specific value from the subject and set as a new header value

If you want to retrieve specific text from the email subject then you can do so by searching the email subject with a Regular Expression that will extract the matching values. The below example will search the email subject for specific content and if found it will then set the value to the relevant email header.

Example Script:

var pattern = new RegExp("example:\\s([0-9]+)").exec(message.getSubject());
if (pattern !== null)
{
    print("Matched Value: " + pattern[1]);
    message.setHeader('example', pattern[1]);
}

Example Test Case:

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: CHANGE_SUBJECT!!! example: 10
From: "ryan example (ryan@example.com)" <system@external.com>
To: test@test.com
Content-Type: text/plain; charset=UTF-8

This is the email body

Test Script:

assert.equal(message.getHeader("example"), '10', 'example header value not found');

Using Regex to match a Lower Case Issue Key and remove the Issue Key from the Subject

Currently, the Regexp that is used by JEMHC to search for matching Issue Key’s is case insensitive which means that even if a Lowercase version of the issuekey is within the subject then it will be used for commenting. The below script will check for a lowercase Issue Key and if one is found then it will remove the matching Issue Key from the email subject, which then allows the issue to be created instead of commenting.

Example Script:

var pattern = new RegExp("([a-z0-9]+-[0-9]+)").exec(message.getSubject());

if (pattern !== null)
{
    print("matched value is: " + pattern[1]);
    message.setHeader("Subject", message.getSubject().replace(pattern[1],""));
}

Example Test Case:

“cm-4” is the example Issuekey

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: CHANGE_SUBJECT!!! cm-4
From: ryan@thepluginpeople.com
To: test@test.com
Content-Type: text/plain; charset=UTF-8

CHANGE BODY!!!

Example Test Script:

assert.equal(message.getSubject(), 'CHANGE_SUBJECT!!! ', 'example header value not found');
  • No labels