JavaScript Pre-Processing Examples
- 1 Available Script Context
- 2 Assertions
- 3 Script examples
- 3.1 Adding an email address to your emails
- 3.2 Detecting a missing From address and adding it to the email
- 3.3 Replacing the From address
- 3.4 Extracting an address from the name part and setting it as the From address
- 3.5 Using Regex to extract a specific value from the subject and set as a new header value
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
|
| |
| 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.
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:
Example Email:
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.
Detecting a missing From address and adding it to the email
Script:
Test Script:
The following assert will check that the From address is now set correctly.
Replacing the From address
This example shows how to manipulate the From address, to detect one domain and swap it out for another.
Script:
Example Email:
Test Script:
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:
Example Test Case:
Test Script:
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:
Example Test Case:
Test Script: