In Closed Beta - not yet publicly available
Available Script Context
message.getBody()
is not available within the JavaScript Script Pre-Processing Task. Only header values can be extracted/set.
API | Notes |
---|---|
| Get all headers of a message. |
| Get value of a message header Parameters
|
| Set the value of a message header Parameters
|
| Adds output that is recorded during execution and shown in the processing report. Parameters
|
| Set the processing outcome of the script Parameters
|
| See below. https://nodejs.org/api/assert.html |
| 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');