I wanted a bit more control over the custom email messages that were trigger by my workflow so I used the following mechanism. In the "Condition and Configuration" section I have something like the following:
// Reads a template file from disk and sets ${mailSubject} and ${mailBody}
import groovy.text.GStringTemplateEngine
// insert additional bindings so template can use (for example) ${issue.reporter.displayName}
def binding = [issue: issue]
String TEMPLATE_PREFIX_PATH = "/some/path/to/email/template/foo"
String SUBJECT_TEMPLATE_PATH = TEMPLATE_PREFIX_PATH + "_subject.vm"
String BODY_TEMPLATE_PATH = TEMPLATE_PREFIX_PATH + "_body.vm"
def engine = new GStringTemplateEngine()
def template = engine.createTemplate(new File(SUBJECT_TEMPLATE_PATH)).make(binding)
config.mailSubject = template.toString()
template = engine.createTemplate(new File(BODY_TEMPLATE_PATH)).make(binding)
config.mailBody = template.toString()
// your condition test goes here
return true
Then in the "Email template" section I have ${mailBody} and the "Subject template" has ${mailSubject}.
Hopes this helps others. Any better ways to do it?
I do a similar thing, but where it makes sense, I also store the template in an Insight object so that it's easy to update.
A couple of things I might suggest
1) Add a try/catch block because if you have a ${variable} in your template that you don't supply in the binding, the script will just crash. You might want to have some fall-back options.
2) Don't use .vm extension. I think that implies a "velocity template" which is a different kind of template with a different language and syntax.
I use the SimpleTemplateEngine instead of the GStringTemplate engine. I think for your specific use case, it would also work. According to the documentation the GStringTemplateEngine "stores the template as writeable closures (useful for streaming scenarios)", I'm not sure what that means in practical terms.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.