Prompt
How do I set up transactional email templates with user data in an app?
Latest observation
To set up transactional email templates with user data in an app, you generally want a system that:
- Creates an email template
- Injects user-specific data into it
- Sends it through an email provider
- Keeps templates safe, testable, and maintainable
Here’s a practical way to do it.
1. Choose an email provider
Use a transactional email service such as:
- SendGrid
- Mailgun
- Postmark
- Amazon SES
- Resend
These providers usually support:
- Template rendering
- Dynamic variables
- Logging and delivery status
- Suppression lists and bounce handling
2. Design templates with placeholders
A template is a reusable email body with variables like:
<p>Hello {{firstName}},</p>
<p>Your order #{{orderNumber}} has shipped.</p>
<p>Track it here: {{trackingUrl}}</p>
Common placeholder styles:
{{variable}}{variable}<%= variable %>- provider-specific syntax
Keep templates:
- Short and focused
- Mobile-friendly
- Plain-text version available too
3. Prepare user data in your app
When an event happens, gather the data needed for the email.
Example:
{
"firstName": "Ava",
"orderNumber": "12345",
"trackingUrl": "https://example.com/track/12345"
}
This data usually comes from:
- User profile
- Order record
- Payment record
- Notification event
4. Render the template with data
There are two common approaches:
A. Render in your app
Your app replaces placeholders before sending:
const template = "Hello {{firstName}}, your order #{{orderNumber}} shipped.";
const emailBody = template
.replace("{{firstName}}", user.firstName)
.replace("{{orderNumber}}", order.number);
This is simple, but can get messy at scale.
B. Let the email provider render it
You send the template ID plus variables:
sendEmail({
to: user.email,
templateId: "order-shipped",
data: {
firstName: user.firstName,
orderNumber: order.number,
trackingUrl: order.trackingUrl
}
});
This is usually better for maintainability.
5. Build a template system in your app
If you manage templates yourself, use a proper template engine:
- Handlebars
- Mustache
- EJS
- Liquid
Example with Handlebars:
<p>Hello {{firstName}},</p>
<p>Your order {{orderNumber}} is on the way.</p>
Then render:
import Handlebars from "handlebars";
const template = "Hello {{firstName}}, your order {{orderNumber}} is on the way.";
const compiled = Handlebars.compile(template);
const html = compiled({
firstName: user.firstName,
orderNumber: order.number
});
6. Store template metadata
Keep template info in a database or config file, such as:
idnamesubjecthtmlTemplatetextTemplateeventTypelocaleactive
Example:
{
"id": "welcome-email",
"subject": "Welcome, {{firstName}}!",
"htmlTemplate": "<p>Hi {{firstName}}, thanks for joining us.</p>",
"textTemplate": "Hi {{firstName}}, thanks for joining us."
}
7. Send the email
Typical flow:
- User action triggers an event
- App loads template
- App fetches user data
- App renders template with data
- App sends email via provider
Example pseudo-code:
const template = await getTemplate("welcome-email");
const user = await getUser(userId);
const html = render(template.htmlTemplate, {
firstName: user.firstName
});
await emailProvider.send({
to: user.email,
subject: render(template.subject, { firstName: user.firstName }),
html
});
8. Handle missing or invalid data
A strong system should handle cases like:
- Missing
firstName - Invalid email addresses
- Null order numbers
- Empty links
Use defaults:
const data = {
firstName: user.firstName || "there"
};
Or validate before sending:
- Required fields
- Email format
- URL format
9. Support localization and personalization
If you have multiple languages or user segments:
- Store templates per locale
- Use the user’s preferred language
- Personalize by plan, region, or behavior
Example:
welcome-email.enwelcome-email.eswelcome-email.fr
10. Test before sending
Important things to test:
- Template renders correctly
- Missing variables don’t break the email
- Links work
- HTML looks good on mobile
- Plain text version is readable
- Special characters are escaped
Use preview tools or a staging environment.
11. Secure user data
Be careful not to expose sensitive information in emails. Avoid including:
- Passwords
- Full payment details
- Secret tokens unless absolutely necessary and short-lived
Also:
- Escape HTML to prevent injection
- Restrict who can edit templates
- Audit template changes if needed
12. Good architecture pattern
A clean setup often looks like this:
- Event layer: emits
user.registered,order.shipped, etc. - Template service: fetches subject/body by event
- Renderer: substitutes variables
- Email service: sends through provider
- Queue/worker: handles retries and async sending
This is especially useful if email sending should not block user actions.
Simple example end-to-end
Template
Subject:
Welcome, {{firstName}}!
Body:
<p>Hello {{firstName}},</p>
<p>Thanks for signing up.</p>
Data
{
"firstName": "Jordan"
}
Send
const subject = render("Welcome, {{firstName}}!", { firstName: "Jordan" });
const html = render("<p>Hello {{firstName}},</p><p>Thanks for signing up.</p>", {
firstName: "Jordan"
});
await emailProvider.send({
to: "jordan@example.com",
subject,
html
});
If you want, I can also show:
- a Node.js example
- a Python example
- how to do this with SendGrid/Mailgun/Postmark
- or a database schema for email templates