To render HTML email templates safely for different users, the main goal is to avoid XSS, content injection, and untrusted HTML/CSS abuse while still allowing personalization.
1) Use a real template engine with escaping by default
Choose a template engine that auto-escapes variables in HTML context.
Examples:
- Python: Jinja2
- Ruby: ERB with proper escaping / Liquid
- Node.js: Nunjucks, Handlebars (with care)
- Go:
html/template
- Java: Thymeleaf, Freemarker with escaping configured
Good pattern
<p>Hello, {{ user.name }}</p>
If user.name is <script>alert(1)</script>, the template engine should output it as text, not HTML.
2) Treat all user-provided content as untrusted
Any content coming from:
- user profiles
- comments
- display names
- custom signatures
- imported CRM data
…should be escaped or sanitized before insertion.
Rule of thumb
- Text only? Escape it.
- Need to allow some formatting? Sanitize it with an allowlist.
3) Sanitize rich HTML before including it
If you must include HTML from users, sanitize it server-side using an allowlist.
Allow only safe tags/attributes, for example:
- Tags:
p, br, strong, em, ul, li, a
- Attributes:
href, title
- Protocols:
https, maybe mailto
Disallow:
<script>
- inline event handlers like
onclick
javascript: URLs
<iframe>, <object>, <embed>
Use a sanitizer such as:
- DOMPurify (Node/browser, with server-side setup)
- Bleach (Python)
- sanitize-html (Node)
- OWASP Java HTML Sanitizer (Java)
4) Never trust CSS from users
Email clients already have inconsistent CSS support, and user-supplied CSS can be abused.
Avoid:
- style attributes from users
<style> blocks from users
- background images or remote CSS from untrusted sources
If you need styling, keep it fully under your control in the template.
5) Avoid raw template insertion
Many template engines have a “raw” or “unescaped” output mode.
Examples:
- Jinja2:
{{ content|safe }}
- Handlebars:
{{{content}}}
Only use these if the content has already been sanitized and you fully trust it.
6) Validate per-user data before rendering
For each placeholder, define what type of data it accepts:
name: plain text
unsubscribe_url: validated absolute URL
discount_code: alphanumeric only
avatar_url: allowlist domain or proxy through your server
This prevents accidental injection and malformed output.
7) Escape URLs separately
If you put user data in links, do not just concatenate strings.
Unsafe
<a href="https://example.com/users/{{ user.id }}">
Safer
- build the URL server-side
- validate the ID format
- escape the attribute value
- ensure only allowed schemes/domains are used
If the URL is user-controlled, whitelist allowed schemes:
Reject javascript:, data:, etc.
8) Use a strict Content Security Policy? Not for email
CSP is great for web pages, but most email clients do not support it reliably. So for email, you should assume no browser-level protection and sanitize everything yourself.
9) Test with malicious payloads
Include test cases like:
<script>alert(1)</script>
<img src=x onerror=alert(1)>
<a href="javascript:alert(1)">click</a>
"><svg onload=alert(1)>
- CSS injection attempts
Verify the rendered email shows these as text or strips them safely.
10) Prefer plain text when possible
If HTML isn’t required, plain-text emails are safest. If you need HTML, send multipart emails with both:
That improves compatibility and provides a safe fallback.
Practical safe rendering strategy
- Render the template with an engine that auto-escapes.
- Only pass prevalidated fields into the template.
- Escape all plain text variables.
- Sanitize any allowed rich HTML using a strict allowlist.
- Never render user HTML directly without sanitization.
- Validate URLs and other structured fields separately.
Example approach in pseudocode
safe_name = escape(user.name)
safe_bio = sanitize_html(user.bio) # allowlist sanitizer
safe_link = validate_url(user.website)
html = render_template("email.html", {
"name": safe_name,
"bio": safe_bio,
"website": safe_link,
})
If you want, I can also show:
- a secure example in Python/Jinja2, Node/Handlebars, or Ruby
- how to safely allow limited rich text
- a checklist for multi-tenant email templates