Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

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:

Code Block
$taskUtils.getHeaderOrCreate($message, 'To').addValue('add.me.as.email.user@mycompany.com')

...

Code Block
$assert.assertEquals('Fernando <fernando@mail.com>,add.me.as.email.user@mycompany.com', $taskUtils.getHeader($message, 'To').getValueAsCSV())

Removing the Date header

Script:

Code Block
$taskUtils.getHeader($message, 'Date).remove()

...

Code Block
$assert.assertNull($taskUtils.getHeader($message, 'Date'))

Detecting a missing From address and adding it to the email

Script:

Code Block
#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

...

Code Block
$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.

...

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.

...

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.

...

Removing Recipients if Combined Addresses Exceed 255 Characters:

If you are using the default email only user custom fields, you may hit the 255 character limit. Which can prevent comments/issues being created. One way around this is to use pre-processing script to remove all recipients if larger than 255 characters.

Example Script:

Code Block
#set ($toCc = "")
#if ($taskUtils.getHeader($message, 'To') != $null) ##Checks if there is a To header, before adding the value to $toCc
    #set($toCc = $toCc + $taskUtils.getHeader($message, 'To'))
#end
#if ($taskUtils.getHeader($message, 'Cc') != $null) ##Checks if there is a Cc header, before adding the value to $toCc
    #set($toCc = $toCc + $taskUtils.getHeader($message, 'Cc'))
#end
#if ($stringUtils.length($toCc)>=255) ##checks character length, if larger than 255 characters then the headers will be removed and replaced.
    #if ($taskUtils.getHeader($message, 'To') != $null)
      $message.removeHeader("To")
    #end
    #if ($taskUtils.getHeader($message, 'Cc') != $null)
      $message.removeHeader("Cc")
    #end
    $message.setHeader("To","catch@email.com") ## Sets the To header with a Catch Email Address, to ensure the email will be processed.
#end

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!!! SCRUM-3
From: euan@thepluginpeople.com
To: catch@email.com, ughsghsdfbjnsjn4upiwncewbgerhgf@gmail.com, jrgiojsdgiojsdgnmwkjbnqjkbsduisdnfwehuiwonfksdmklfm@gmail.com, hgsiuhgkfnsgkrjnguhtnvfmdlkaiunjfgndsvjipeuipnvui@gmail.com, hgtgrnemgkfehgishdnafjklqgnuihgu4pqnfuipngkrjan4tuingufdahfjkenqgfrlpsjfiaqphgwpnug@hotmail.com iregnfsadjlfgbreiupgbnjerkgnfjsgbwqpf4ufnrbvjfnblvjdsfbgjsgbnsfdlbgjfnlsvjkanvrjk@gmail.com
CC: gmail@gmail.com
Content-Type: text/plain; charset=UTF-8

CHANGE BODY!!!

Test Script:

Code Block
$assert.assertEquals('test@test.com', $taskUtils.getHeader($message, 'To').getValueAsCSV())

Script Configuration:

...