Velocity Script
When creating a Script you may need to use a specific method that will perform specific actions. These methods can be found by expanding the Velocity Context section. Here you will see some pages that contain methods that you may need to use. When you expand the Velocity Context you will also find links to Javadoc for the published Java classes to help guide you.
Script examples
Adding your mailbox email address
Script:
$taskUtils.getHeaderOrCreate($message, 'To').addValue('add.me.as.email.user@mycompany.com')
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.assertEquals('Fernando <fernando@mail.com>,add.me.as.email.user@mycompany.com', $taskUtils.getHeader($message, 'To').getValueAsCSV())
Removing the Date header
Script:
$taskUtils.getHeader($message, 'Date).remove()
Test Script:
The following assert will check whether a value is returned. The Test Script passes if the returned value is now Null.
$assert.assertNull($taskUtils.getHeader($message, 'Date'))
Detecting a missing From address and adding it to the email
Script:
#set ($original = $message.getFrom()[0].getAddress().toLowerCase()) ##Extracts the Value found within the From header #if ($stringUtils.isEmpty($original)) ##Checks if From address is missing $message.setFrom("newaddress@example.com") ##Sets the From address #end
Test Script:
The following assert will check that the From address is now set correctly.
$assert.assertEquals('newaddress@example.com', $taskUtils.getHeader($message, 'from').getValueAsCSV())
Replacing the From address
This example shows how to manipulate the From address, to detect one domain and swap it out for another.
Script:
#set ($original = $message.getFrom()[0].getAddress().toLowerCase()) ##Extracts the Value found within the From header #if ($original.endsWith("@thepluginpeople.com")) ##Checks if sender address is a specific address #set ($name = $original.split("@")[0]) $message.setFrom($name + "@replaced.com") ##Changes original from address to be the extracted name and email address #end
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:
$assert.assertTrue($message.getFrom()[0].getAddress().equalsIgnoreCase("andy@replaced.com"))
Script Configuration:
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:
#set ($original = $taskUtils.getHeaderValue($message, "from").toString()) ##Extracts the Value found within the From header #if ($original.contains("system@external.com")) ##checks if sender address is a specific address #set ($address = $stringUtils.substringBetween($original, "(", ")")) ##Extracts the address from between the two brackets "("")" #set ($personal = $stringUtils.substringBetween($original, '"', "(")) ##Extracts the real senders personal part (Name) $message.setFrom('"' + $personal + '"' + "<"+ $address +">") ##Changes original from address to be the extracted name and email address #end
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.assertTrue($message.getFrom()[0].getAddress().equalsIgnoreCase("ryan@example.com")) $assert.assertTrue($message.getFrom()[0].getPersonal().equalsIgnoreCase("ryan example"))
Script Configuration:
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.