Creating an email template

December 21, 2021

This document describes how to create your own email template. Templates can be used by Forms actions such as ‘Send Email Action’. In order for Forms to discover custom email templates, we need to modify our pom.xml and add Forms as a dependency of our module.

Dependency Requirement

In pom.xml:


           <!--Higher priority than form-factory-core (2)-->

Velocity Template Language

Templates are written in Velocity Template Language (VTL). These files can be identified by the extension .vm and allow us to access java code. Within your action(ex: SendEmailAction) you can bind variables to the template, which can then be accessed and expressed.

Folder Structure

Templates should be created under the src/main/resources/templates/email directory. By default templates folder does not exist in a newly created module. If that is the case, it must be created and the subdirectory ‘email’ under it.

custom.vm will be the name of our custom template.

Template Parameter Binding

In our action(example taken from Send Email Action) we can pass parameters to our template by creating a Map<String, Object> Object; Store our parameters within it and pass the map as a parameter to sendMessageWithTemplate method of class.

For a more convenient approach, we can create an instance of org.jahia.modules.formfactory.actions.models.Email class and use the addBinding method, to store all the parameters that we want available within our template(see Send Email Action for an example).


Within the template we can declare the variable by:

#* @vtlvariable name="formName" type="java.lang.String" *#
#* @vtlvariable name="formDatas" type="java.util.Map" *#
#* @vtlvariable name="bundle" type="java.util.ResourceBundle" *#
#* @vtlvariable name="emailBodyMessage" type="java.lang.String" *#

And make use of the binding(s) as follows:

<table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="result_table">
   <tbody id="tableBody">
       #foreach( $it in $formDatas.keySet())
               <td>&nbsp;<label style="font:bold"> $it</label>&nbsp;</td>

Template Selection

Using the Send Email Action as an example, we can configure the action to use our custom template by selecting it from the dropdown menu(located in the action configuration panel):

The emails sent by Send Email Action will now use our custom.vm template.