Generating HTML using document template for email body

To send an email confirming an order, I tried to achieve the following: Generate HTML content using a document template Use the generated content as HTML body of an email. The email is supposed to contain an order confirmation of my (fictional) web shop. As all major webshops do, I want it to contain an order summary and a line for each ordered product, including a small product image. I really like to use templates because: they support one to many relations. any images get wrapped as base64 encoded data. they are multilingual Drawback is that they can only be changed in the modeler, where email templates can be changed any time. But in an agile environment, getting a template changed should not take too long. With a little Java help, it is working but the HTML generation is not what I expected: Appearance - The document template has several properties in the appearance section, including a default font, set to Arial. This does not work, because of the pre tags in the HTML, which revert the font styling to ancient looking monospace. This could be fixed by generating the default font as pre {font-family: arial;} in the generated style tag in the header of the HTML. Any custom styling, like font, size or width, on individual elements or tables is lost. Table column weights are not used so the columns are sized to the largest item in each column without any further spacing. I even tried using dynamic labels with render XHTML = true. It does render the HTML tags, but any attributes on the tags get stripped. The fallback is of course to generate a PDF and attach that to the email, which works just fine. But it could be much better. I found a few similar questions on the forum, from a while ago, which did not get a reply. I really hope that these issues can get fixed as the EmailTemplate module and document templates can be a very powerful combination.
2 answers

We did something like this. We use a document template to generate a HTML file. Then use the CommunityCommons.StringFromFile action to extract the contents of the file to a string. The string is used as a body text of an email. It does not offer extensive layout functionality, however it does generate the dynamic multi row table that we require (list of overdue invoices). We added 3 regions in the template: - header (containing a text field populated by a Rich Text editor generated piece of html content), - middle (containing the dynamic table) - footer (containing a text field populated by a Rich Text editor generated piece of html content).


Well that proved easier than anticipated :-) A little too big for a comment though.

A few replaceAll calls in a Java action and it looks much better.

I have put additional styling in the header like this: replace </style> with pre { font-family: arial; font-size: 10pt; }</style> Bye bye monospaced font! :-)

I also put some static labels in strategic places in the template, like an additional row in a table. I use @LT and @GT@ in the captions, these get replaced in Java too so I can use custom styling.

Still, Mendix does the heavy lifting and I only need to tweak the result a bit.