Saturday, November 24, 2012

Generating the Email content with a Template based mechanism

Emailing is one of the most critical as well as important functionalities in any software service or a system. Furthermore the email content may well change depending on the issue you are addressing at a given time. Having to handle such scenarios via the code could be messy and it would take lot effort to maintain such a code. Furthermore when new email content gets added or the content of an existing email format changes, then you may have to go into the code level to do the necessary changes.

To overcome the above mentioned drawbacks it would be really great if there is a way where we can feed the email format as a template from outside, bind the relevant information to the template variables via the code and at the end convert the template into the email body. In one of the ongoing projects in our team we had exactly the same requirements as well as the same drawbacks. So in order to find a good solution to this I did some Goggling and tried to find out a way where we can get things done through a template based mechanism.
 The two prominent solutions that appeared in the search results were ‘FreeMarker’ and ‘Apache Velocity’. After going through each of them I noticed that ‘Apache Velocity’ fulfills all our requirements and resolves all our drawbacks.

‘Velocity’ is a Java based project that operates under Apache and like many apache projects ‘Velocity’is backed up by a pretty strong development and user community.
 Apart from supporting template based email content generation it also supports the features that are listed here. 
  •  Servlet-based Web applications 
  • Java and SQL code generation
  • XML processing and transformation
  • Text processing, such as RTF file generation
Mentioned below is a quick description on how velocity can be adapted in to your project. This is a screenshot of a template that is used by Velocity. The template file extension is ‘vm’. 



It is a template that supports HTML based content; therefore you have the option of formatting the outlook of your email body in whatever way you want. Furthermore it supports basic programming stuff like for loops,foreach, if-else statements, etc... This makes the template much easier to generate.




After initializing the velocity engine you can bind the values to the variables in the template as you are adding items into a Java hash map through the ‘VelocityContext’ object. 


When adding content to the HTML table, its rows should be added to a Hashmap where number of elements in the hashmap would represent the table columns. Then each row(HashMap objects) should be added to a ArrayList and then as the final step that array-list object should be added to the context object .

After filling the variables in the template now it is time to convert the template in to something that can be added to your email body.


Now the template with its bound variables is filled in to a StringWriter which can be used as the body of your email.
When using the downloaded Velocity.jar as a library of your project you need to have couple of runtime libraries from the Apache-Commons library collection (commons-collections-3.2.1.jar, commons-lang-2.6.jar).  For those who are not so familiar with apache-commons it is a collection of reusable Java components, which has libraries for scenarios like String manipulation, Database operations, logging and many more.
In case you need more information there are lots of articles in the internet for Velocity as well as for Commons.

No comments: