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');