Back to blog

Dynamic email tables

M. Pecho
M. Pecho
Updated:

Each customer's order summary can contain a different number of items. In doMail, you can link order data to its items and use FreeMarker to create a separate table for each recipient. This example uses two CSV files and one HTML template that also calculates totals.

Prepare orders and their items

The main file, orders.csv, contains one row per order: its identifier, email address and customer name. The additional file, order_items.csv, contains the individual items with their quantities and unit prices. Both files have column names in the first row.

The OrderId column links the files. Order ORD-2026-1001 has four items, and order ORD-2026-1002 has two. The order identifier also distinguishes multiple orders belonging to the same customer. Each row in the main file represents a separate message.

Two orders in orders.csv and six items in order_items.csv, linked through the OrderId column.

Link the data sources in the template

Under Configuration → Templates, create and save a template. On the Template data tab, upload orders.csv as the main source. Select a comma as the separator and indicate that the file has headers. Keep the column names OrderId, Email and CustomerName, then accept the settings.

Upload order_items.csv under Additional sources and configure the separator and headers in the same way. For its OrderId column, select the main source's OrderId in Connection to main table. Accept and save the changes.

When building a message, doMail selects additional rows with the same key value. The values must therefore match exactly in both files. For this example, also keep the other column names so they match the expressions in the supplied HTML template.

The orders.csv and order_items.csv sources in doMail, linked to the main table through OrderId.
The main and additional data sources are linked through OrderId. Click to open a larger image.

Build the table with FreeMarker

On the Design tab, upload the prepared file using the HTML button. The file is an HTML fragment intended for import into the editor. Use expressions such as ${CustomerName} or ${OrderId} for values from the main source. The items are available in the order_items list, whose name comes from order_items.csv without its extension.

The prepared template creates a row for each item and calculates the total quantity and amount, summing quantity multiplied by unit price. If an order has no items, a short message is displayed.

Values from CSV are converted to numbers using ?trim?number. The file therefore uses a decimal point, for example 3.50. The 0.00 format displays prices with two decimal places. The complete template sets English number formatting and HTML escaping for inserted text using the outputformat and autoesc blocks.

FreeMarker control directives are stored as escaped tags inside HTML comments. This keeps the editor from moving them outside the table or removing their closing tags during import and saving. doMail decodes them before processing. Do not use an <#ftl> header for this import: after saving through the editor, it is no longer at the start of the resulting template.

For further changes to expressions, see the Apache FreeMarker documentation.

The result for each recipient

The same design produces two different messages. Alex Morgan receives four items with a total quantity of 11 and an amount of EUR 67.80. Sam Taylor receives two items with a total quantity of 3 and an amount of EUR 42.70. Each message contains the items assigned to its order.

Two emails generated from the same template: Alex Morgan's order totals EUR 67.80 and Sam Taylor's order totals EUR 42.70.

For the steps to create and start a campaign, see How to create and launch an email campaign in doMail.