Francisco Canedo
Francisco Canedo

Reputation: 2010

What's a good html e-mail template system that also, correctly, renders a text/plain alternative?

We have a web application that periodically sends out e-mails to users. At the moment we generate the html version and the text version in the code. However, this is cumbersome to maintain.

Is there a good e-mail template system out there that can generate both the html and text versions of an e-mail from the same template for Java?

Some requirements:

In case it matters to your answer, we're using Struts… <cough>1</cough>.

Upvotes: 4

Views: 2365

Answers (4)

Jeroen van Bergen
Jeroen van Bergen

Reputation: 1046

I would also recommend the use of a template engine. Create two templates, one for the text part and one for the HTML part. Use the include functionality of the template engine so you can re-use literal texts in both templates.

Upvotes: 2

zvikico
zvikico

Reputation: 9825

You could use a utility which transforms HTML to Text in Java (Google for it, for example this one) by stripping tags and converting the special HTML chars. However, this will not give you everything you need, especially not formatting (like lists) and links.

Another option is to use an XSLT to transform your XHTML (write it properly...) to text and use an XSLT processor (like Xalan-J or Saxon) to run it. This is a fairly simple XSLT exercise as long as your requirements are simple (e.g. you don't care about CSS issues).

Upvotes: 2

guerda
guerda

Reputation: 24059

Take the Apache Velocity. I don't know a more powerfull template engine.

Upvotes: 0

Boris Pavlović
Boris Pavlović

Reputation: 64632

I have successfully used Freemarker templates for preparing txt and html content for emails. If you use Spring it can make things even easier since it has all necessary plumbings. Even without Spring, JavaMail API is user friendly so it shouldn't be a problem.

Upvotes: 0

Related Questions