Article
· Jul 29 3m read

Dynamic Templated Emails in InterSystems IRIS with templated_email


Sending emails is a common requirement in integration scenarios — whether for client reminders, automatic reports, or transaction confirmations. Static messages quickly become hard to maintain and personalize. This is where the templated_email module comes in, combining InterSystems IRIS Interoperability with the power of Jinja2 templates.

Why Jinja2 for Emails

Jinja2 is a popular templating engine from the Python ecosystem that enables fully dynamic content generation. It supports:

  • Variables — inject dynamic data from integration messages or external sources
  • Conditions (if/else) — change the content based on runtime data
  • Loops (for) — generate tables, lists of items, or repeatable sections
  • Filters and macros — format dates, numbers, and reuse template blocks

Example of a simple email body template:

 


Hello {{ user.name }}!

{% if orders %}
You have {{ orders|length }} new orders:
{% for o in orders %}
 - Order #{{ o.id }}: {{ o.amount }} USD
{% endfor %}
{% else %}
You have no new orders today.
{% endif %}
 

With this approach, your email content becomes dynamic, reusable, and easier to maintain.

Email Styling and Rendering Considerations

Different email clients (Gmail, Outlook, Apple Mail, and others) may render the same HTML in slightly different ways. To achieve a consistent appearance across platforms, it is recommended to:

  • Use inline CSS instead of relying on external stylesheets
  • Prefer table‑based layouts for the overall structure, as modern CSS features such as flexbox or grid may not be fully supported
  • Avoid script and complex CSS rules, as many email clients block or ignore them
  • Test emails in multiple clients and on mobile devices

Capabilities of the templated_email Module

The templated_email module is designed to simplify creating and sending dynamic, professional emails in InterSystems IRIS. Its key capabilities include:

  • render Jinja2 templates to generate dynamic email content
  • Flexible usage — can be used both from Interoperability productions and directly from ObjectScript code
  • Built‑in Business Operation — ready‑to‑use operation for production scenarios with SMTP integration
  • Automatic inline styling — converts CSS styles into inline attributes for better email client compatibility
  • Markdown support — allows creating clean, maintainable templates that are rendered to HTML before sending

These features make it easy to produce dynamic, well‑formatted emails that display consistently across clients.

Module templated_email

The module provides the following key components:

  • TemplatedEmail.BusinessOperation — a Business Operation to send templated emails via SMTP
  • TemplatedEmail.EmailRequest — an Interoperability Message for triggering email sends, with fields for templates, data, and recipients
  • TemplatedEmail.MailMessage — an extension of %Net.MailMessage that adds:
    • applyBodyTemplate() — renders and sets the email body using a Jinja2 + Markdown template
    • applySubjectTemplate() — renders and sets the email subject using a Jinja2 template

This allows you to replace %Net.MailMessage with TemplatedEmail.MailMessage in ObjectScript and gain dynamic templating:


set msg = ##class(TemplatedEmail.MailMessage).%New()
do msg.applySubjectTemplate(data,"New order from {{ customer.name }}")
do msg.applyBodyTemplate(data,,"order_template.tpl")

More details and usage examples can be found in the GitHub repository.

Templates Are Not Just for Emails

The template rendering methods can also be used for generating HTML reports, making it easy to reuse the same templating approach outside of email.

With templated_email, you can create dynamic, professional, and maintainable emails for InterSystems IRIS — while leveraging the full flexibility of Jinja2 templates.

Discussion (0)1
Log in or sign up to continue