Mastering Jenkins: Editable email notification templates [Email-EXT]


One more widely adopted Jenkins plugin is the Email-EXT plugin provided by the generous folks within the Jenkins open source community. The Email-EXT plugin specifically allows Jenkins administrators to configure and manipulate almost every aspect of an email notification within a Jenkins job. For those interested the link for the plugin can be found [here]. The documentation on the plugin itself is quite comprehensive and it would not be wise to re-cover what’s already pretty well documented. What we will be talking about in this specific section of Mastering Jenkins is the implementation of actual notification templates. Some examples, some pitfalls and some tips that might help you make use of this plugin more rapidly.

Some basic example templates (provided by the community)

Buried within the documentation page for this plugin is a well hidden link to some example templates. That link is provided here. These should get you up and running quickly.


The above documentation and templates gave me a good starting point; but it didnt seem comprehensive or practical by any means. So I spent time developing my own templates, which I will share here. While I could have used Groovy to capture job data and used Jelly templates to format it into HTML I wanted something a bit more encapsulated to the job itself. This was the reasoning behind my own implementations.

Overcoming adversity

One of the items I struggled with (and others too apparently) was to capture the job executions console output, and format it properly in the email notification. The main issue I had with this was with line endings. Apparently ‘\n’ does not translate well to HTML. In HTML we expect the new-line to be represented using a < br /> tag but our console output is not formatted HTML. So unless you replace all occurrences of \n with < br > you end up with a jumbled looking console output in your email notification.  After a couple of asprin I managed a solution that worked.

The solution to this issue was to use the REGEX REPLACE function provided by the authors of the plugin. Apparently you can configure the email template to perform a mass regular expression replace of the console output on the fly and pipe the output to your email template. What stumped me for a while was that when searching the end of a line for a \n it would not find it. This still leaving me with a jumbled mess.  Thus the solution was instead to search for a newline character at the beginning of each line instead.  The code I used is provided below.


Beyond the basics

I spent some time recently generating (what I think) are pretty comprehensive SUCCESS and FAILURE notification templates, which I would like to share with the community and my readers.  This is provided in HTML code form below.






Leave a Reply