Prompt
How do I add email templates to my product?
Latest observation
To add email templates to your product, you typically need 4 pieces:
- A template format
- A place to store templates
- A rendering system to fill in variables
- An email-sending service
Here’s a practical way to build it.
1) Decide what kind of templates you need
Most products need a few email types, such as:
- Welcome email
- Password reset
- Invoice receipt
- Notification alerts
- Marketing campaigns
For each template, define:
- Subject
- Body
- Variables like
{{first_name}},{{reset_link}} - HTML and/or plain text version
Example:
Subject: Reset your password
Hi {{first_name}},
Click here to reset your password:
{{reset_link}}
If you didn’t request this, ignore this email.
2) Choose a template engine
You’ll want placeholder substitution and conditional logic. Common options:
- Handlebars
- Liquid
- Mustache
- Jinja2 (Python)
- ERB (Ruby)
A good rule:
- Use a simple templating language if non-developers will edit templates
- Use full code-based templates if only engineers will manage them
3) Store templates somewhere
You have a few options:
Option A: In your codebase
Best if templates change rarely.
- Easy to version control
- Simple deployment
- Harder for non-technical users to edit
Example:
emails/welcome.htmlemails/welcome.txt
Option B: In a database
Best if templates need to be editable in-app.
Store fields like:
namesubjecthtml_bodytext_bodyvariableslocaleupdated_at
Option C: In an external provider
Use tools like:
- Postmark
- SendGrid
- Mailgun
- AWS SES
- Customer.io
- Braze
These often support template management, rendering, and analytics.
4) Render the template with data
When sending an email, pass in the data needed for placeholders.
Example data:
{
"first_name": "Ava",
"reset_link": "https://example.com/reset?token=abc123"
}
Rendered output:
Hi Ava,
Click here to reset your password:
https://example.com/reset?token=abc123
Make sure to:
- Validate required variables
- Provide defaults for optional ones
- Escape user input in HTML templates to prevent injection
5) Send via an email provider
Don’t send email directly from your app server if you can avoid it. Use a provider:
- Resend
- SendGrid
- Postmark
- Mailgun
- AWS SES
Why:
- Better deliverability
- Bounce handling
- Spam complaints tracking
- Retry logic
- Dedicated APIs
6) Add preview and testing
Useful product features:
- Preview template with sample data
- Send test email
- Validate required variables
- Template version history
- A/B testing for marketing emails
- Localization for different languages
7) Build a simple architecture
A common setup:
- Admin UI for editing templates
- Template storage in DB or provider
- Renderer service to substitute variables
- Mailer service to send the final email
- Queue/worker for asynchronous sending
Flow:
- User triggers event
- Your app selects the right template
- Template is rendered with data
- Email is sent through provider
- Delivery/bounce events are logged
8) Example implementation approach
If you want a simple internal solution
- Store templates in your database
- Use a templating engine like Handlebars/Liquid
- Send via Postmark/Resend/SendGrid
- Build a basic admin screen for editing templates
If you want to move fast
- Use your email provider’s built-in templates
- Keep only template IDs in your product
- Render and send through their API
9) Important best practices
- Keep transactional and marketing emails separate
- Always include:
- company name
- reply-to info
- unsubscribe link for marketing emails
- Ensure mobile-friendly HTML
- Test across clients: Gmail, Outlook, Apple Mail
- Log every send with template name/version
- Handle missing variables gracefully
- Support multi-language templates if needed
10) Minimal data model example
templates
- id
- name
- subject
- html_body
- text_body
- locale
- type
- created_at
- updated_at
And a send log:
email_logs
- id
- template_id
- recipient
- status
- provider_message_id
- sent_at
- error
Recommended MVP path
If you’re just starting:
- Pick one email provider
- Use Handlebars or Liquid
- Store templates in your database
- Add a preview/test send feature
- Send emails through a background job queue
That gives you flexibility without too much complexity.
If you want, I can also give you:
- a sample database schema
- a Node/Python/Ruby implementation
- or a recommended architecture for SaaS products