Send an email with Dynamic Table content displayed in it's Body
Good morning ,
So here is the case :
-I want to figure out how to include a table (template that is gonna be filed with dynamic information retrieved from a backend service) in the body of an email sent from Bonita for different users (maybe we will use a connector in this case).
-The content of the table must be displayed in the body of the mail , not as an attachment.
Is there any suggestions to achieve this ?
1 answer
Hi, you may use a Groovy script expression as message body with Groovy multi-string:
"""
<p>Hello ${user.firstName}, some more email html content</p>
<table>
<thead>
<tr>
<th colspan="2">A Table example</th>
</tr>
</thead>
<tbody>
<tr>
<td>First Name</td>
<td> ${user.firstName}</td>
</tr>
<tr>
<td>Last Name</td>
<td> ${user.lastName}</td>
</tr>
</tbody>
</table>
""".toString()
<p>Hello ${user.firstName}, some more email html content</p>
<table>
<thead>
<tr>
<th colspan="2">A Table example</th>
</tr>
</thead>
<tbody>
<tr>
<td>First Name</td>
<td> ${user.firstName}</td>
</tr>
<tr>
<td>Last Name</td>
<td> ${user.lastName}</td>
</tr>
</tbody>
</table>
""".toString()
Where user
is a business or process variable.
Do not forget to enable html content checkbox in the connector configuration.
If you want to do more advanced templating, Groovy has built-in features for that !
HTH
Romain
Comments
Thank you for your answer !
I wanna ask you , It won't be an issue if the body message is too long for an email ( 6 columns and 26 rows ) , is there an option to store that dynamic content in a file or smth and then send it back as en email content ?
This is a responsiveness issue with html table (that's why they are not used a lot anymore). Have a look here for html tips.
Yes, you can imagine reading the template from an external source. By example you may have a template file
template.groovy
stored on a public github repository.template.groovy
<body>
<p>Hello ${user.firstName} ...etc</p>
</body>
</html>
And use the groovy template engine like this:
new SimpleTemplateEngine()
.createTemplate('https://raw.githubusercontent.com/you/mail-templates/template.groovy'.toURL())
.make([ //Create a map with the variable used in the template
user:user
])
.toString()
What about the aspect of document in Bonita , what is used for ? Can we use it to store the template in it or smth?
You could, but I don't think it is a good idea as it will then be stored in database and quite painful to update.
alright , thank you for your answer.