...
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
Textual Assertions
See https://nodejs.org/api/assert.html for full Assert API documentation.
Textual Assertions
Code Block |
---|
assert.equal('The actual value', 'An expected value', 'Helpful message'); |
...
Running the above Test Script example against any email will produce the following output :
...
If (if you do not with to provide a message you can simply omit it.)
Code Block |
---|
assert.equal('The actual value', 'An expected value'); |
...
Code Block |
---|
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:
Code Block |
---|
var to = message.getHeader('to');
var newTo = to + ', someone@example.com';
message.setHeader('to', newTo); |
Example Email:
Code Block |
---|
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.
Code Block |
---|
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:
Code Block |
---|
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.
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
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
Code Block |
---|
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:
Code Block |
---|
assert.equal(message.getSubject(), 'CHANGE_SUBJECT!!! ', 'example header value not found'); |