Please enable JavaScript to view this site.

InterFormNG2 Manual

Navigation: Library

Mail templates library

Scroll Prev Top Next More

You probably want to configure the contents, when you send an email with InterFormNG2.

 

In InterFormNG2 this can be defined as either an html file (which is recommended) or as an Apache Velocity Template (.vm), which is described below.

 

 

Email template as .vm

Apache Velocity templates are in short an extension to html. This extension makes it e.g. possible to refer to variables in the html and also to refer to an input XML file inside the .vm file.

The recommended way to create a .vm file is to first create the equivalent html file (as far as it goes), and then add the commands specific for Apache Velocity templates.

 

The user guide for Apache Velocity Templates is found here:

https://velocity.apache.org/engine/2.0/user-guide.html

 

You can check out the demo mail template called defaultMailTemplate.vm to see how a Velocity Template can be used.

 

The mail template can be imported in the Library and referred to by these email workflow components:

 

1.Create PDF email (Used for simple emailing).

2.Create EML with PDF (Used for more advanced email and to create .eml files).

 

There are many functions in Velocity Templates, and below I show a few:

 

1.Use a simlpe variable.

2.Refer to the contents of an input xml file.

3.Use conditions in Velocity templates.

4.Output detail lines of an input xml file.

 

Use a simple variable

You can refer to variables in .vm files by preceding the variable name with a dollar sign e.g. like $MyVar. Remember that variables are case sensitive.

Consider a .vm file with these lines:

 

<h1 style="color: #4485b8;">Hello World</h1>

<p>Dear $ContactPerson. </p>

 

If you combine that with a workflow, where a value for the variable, ContactPerson has been set before the email is built, then you can setup a dynamic email, where you address the customer by the name of the person:

 

NG2MailTemplate0001

 

This is below used with the Create EML with PDF workflow component:

 

NG2MailTemplate0002

 

The result looks as expected:

 

NG2MailTemplate0003

 

 

Refer to contents of an input XML file

It is possible to refer to data from an input XML file, that is stored in the workflow payload.

 

If you consider an XML file with this as the contents:

 

NG2MailTemplate0005

 

Then we might want to refer to the Contact_Name from the input file in the email contents. One way to do that, is to change the example above and set that in the ContactPerson variable, but it is also possible to refer directly to the XML without a variable.

 

To do that you can use the GetChild() and getText() functions to retrieve the value of the Contact_Name from the XML file above with this setup:

$root.getChild("CompanyInfo").getChild("Contact_Name")).getText()

 

The getChild() function set the current node to be be child with the specified name, and the getText() function retrieves the value of the current node.

 

So with that in mind we can use a .vm file with this as the contents to include the Contact_Name from the input file:

 

<h1 style="color: #4485b8;">Hello World</h1>

<p>$root.getChild("CompanyInfo").getChild("Contact_Name")).getText().</p>

 

The matching Workflow is here:

 

NG2MailTemplate0006

 

You may notice, that we do not update any variable.

 

The output is as expected:

 

NG2MailTemplate0003

 

 

Use conditions in Velocity Templates

Conditions can also be added in the Velocity Templates.

 

In the example below we want to add one of two greetings in the email depending on if the contact person is Jens Hansen or not. The contact person is assumed to be available as the variable, ContactPerson as in the first example above.

 

In this example we include the name, but we also want to include a personal greeting to Jens Hansen, if he is the contact person:

<h1 style="color: #4485b8;">Hello World</h1>

<p>Dear $ContactPerson. </p>

#if( $ContactPerson == 'Jens Hansen' )

 <p>Long time no see.</p> 

#else

 <p>Glad to hear from you.</p> 

#end

 

With the if..else above the result will depend on the same of the contact person. Here are the results for both Jens Hansen and Hans Hansen:

 

NG2MailTemplate0007

 

NG2MailTemplate0008

 

 

Output detail lines of an input XML file

It is possible to output a complete input XML file as the email contents. One important part of that is to be able to handle the detail lines of the input file.

For that we need to define an html table and introduce a loop in order to loop through all detail lines of the input file.

 

I have an example below to illustrate how this can be done.

 

Consider this XML file:

 

NG2MailTemplate0009

In this XML file we have an unknown number of detail lines, and we want to iterate across them all and output the data in a table.

 

This can be achieved with a .vm mail template file with this as the contents:

 

NG2MailTemplate0010

 

Inside the table there is a foreach loop, where the variable, Line iterates across all the /DetailLines/DetailLine node and points to each of the nodes in turn.

 

The line:

$Line.getAttributeValue("SEQ") retrieves the value of the SEQ attribute of the current DetailLine.

 

The other lines retrieves the values of the specified children of the current DetailLine.

 

If you use this mail template in a workflow for emailing e.g. like in this example above, then the contents of the email will be as expected:

 

NG2MailTemplate0011