Prompt
How do I build email templates with variables for a SaaS app?
Latest observation
A good way to build email templates for a SaaS app is to separate:
- Template content
- Variable data
- Rendering logic
- Delivery system
That gives you reusable emails, easy customization, and safer maintenance.
1) Choose a template format
Common options:
- Handlebars / Mustache: simple variables and basic conditionals
- MJML: makes responsive email HTML easier
- React email: component-based, good if your app already uses React
- Liquid: popular in SaaS and marketing tools
- Plain HTML + placeholders: simplest, but gets messy fast
A common setup is:
- Use MJML or React Email for layout
- Use Handlebars/Liquid-style variables for dynamic text
Example placeholder style:
<p>Hello {{first_name}},</p>
<p>Your invoice {{invoice_number}} is ready.</p>
2) Define your variables clearly
Create a schema for each template so you know what data it expects.
Example:
{
"first_name": "Jane",
"company_name": "Acme Inc",
"invoice_number": "INV-1024",
"cta_url": "https://app.example.com/invoices/INV-1024"
}
Best practices:
- Use consistent naming like
first_name,company_name - Keep variables specific to the template
- Provide defaults for missing values
- Support conditional sections for optional fields
Example:
{{#if first_name}}
Hi {{first_name}},
{{else}}
Hi there,
{{/if}}
3) Store templates in a manageable way
You have a few choices:
Option A: In code
Best for:
- small teams
- product emails
- version control
Store templates in your repo as files:
welcome_email.mjmlpassword_reset.hbs
Option B: In a database or CMS
Best for:
- non-developers editing templates
- marketing teams
- frequent changes
Store:
- template name
- subject line
- body HTML
- version
- enabled status
Option C: Hybrid
- Core transactional templates in code
- Marketing templates in an admin UI or CMS
This is often the best SaaS approach.
4) Render templates server-side
When sending email:
- Fetch the template
- Merge it with variables
- Render HTML/text
- Send through email provider
Pseudo-flow:
const template = getTemplate("welcome_email");
const html = render(template.html, {
first_name: user.firstName,
company_name: user.companyName
});
sendEmail({
to: user.email,
subject: "Welcome to ExampleApp",
html
});
Important:
- Always render on the backend
- Never let clients directly construct raw email HTML for production sends
- Escape variables by default to prevent injection
5) Include both HTML and text versions
Email deliverability improves when you send:
- HTML email
- plain-text fallback
Example:
- HTML: designed template
- Text: simple readable version
Many libraries can generate the text version automatically.
6) Support reusable components
For SaaS emails, create shared pieces like:
- header
- footer
- buttons
- legal block
- brand styles
This prevents repeated code across templates.
Example components:
EmailLayoutPrimaryButtonFooterLinks
7) Handle localization
If you serve multiple languages:
- Keep template keys separate from copy
- Pass localized strings into templates
- Avoid hardcoding language in HTML
Example:
{
"subject": "Reset your password",
"greeting": "Hello",
"cta_text": "Reset password"
}
8) Version your templates
Emails often change over time. Track:
- template version
- last updated
- who edited it
- rollback option
This helps prevent broken emails after updates.
9) Test rendering before sending
Test with:
- missing variables
- long names
- special characters
- mobile clients
- dark mode
Use preview data:
{
"first_name": "Alex",
"invoice_number": "INV-9999"
}
10) Use an email service provider
For sending, use:
- SendGrid
- Mailgun
- Postmark
- Amazon SES
- Resend
These can handle:
- SMTP/API delivery
- bounce tracking
- open/click tracking
- templates in some cases
Recommended architecture for a SaaS app
A practical setup:
- Template files in repo or DB
- Variable schema per template
- Backend renderer using Handlebars/Liquid/React Email
- Email provider for delivery
- Preview/testing tool for QA
- Versioned templates for safety
Example simple implementation
Template
<!doctype html>
<html>
<body>
<h1>Welcome, {{first_name}}!</h1>
<p>Your account for {{company_name}} is ready.</p>
<a href="{{cta_url}}">Get started</a>
</body>
</html>
Data
{
"first_name": "Sam",
"company_name": "Acme",
"cta_url": "https://app.example.com/start"
}
Render and send
import Handlebars from "handlebars";
const template = `
<h1>Welcome, {{first_name}}!</h1>
<p>Your account for {{company_name}} is ready.</p>
<a href="{{cta_url}}">Get started</a>
`;
const html = Handlebars.compile(template)({
first_name: "Sam",
company_name: "Acme",
cta_url: "https://app.example.com/start"
});
My recommendation
If you're building a SaaS app from scratch:
- Use Handlebars or Liquid for variables
- Use MJML or React Email for layout
- Render server-side
- Store template metadata + version + variable schema
- Keep HTML + text versions
- Add a preview/testing workflow
If you want, I can also give you:
- a database schema for email templates,
- a Node.js implementation, or
- a React Email / MJML example.