`, the template engine should output it as text, not HTML.\n\n## 2) Treat all user-provided content as untrusted\nAny content coming from:\n- user profiles\n- comments\n- display names\n- custom signatures\n- imported CRM data\n\n…should be escaped or sanitized before insertion.\n\n### Rule of thumb\n- **Text only?** Escape it.\n- **Need to allow some formatting?** Sanitize it with an allowlist.\n\n## 3) Sanitize rich HTML before including it\nIf you must include HTML from users, sanitize it server-side using an allowlist.\n\nAllow only safe tags/attributes, for example:\n- Tags: `p`, `br`, `strong`, `em`, `ul`, `li`, `a`\n- Attributes: `href`, `title`\n- Protocols: `https`, maybe `mailto`\n\nDisallow:\n- ``\n- ``\n- `click`\n- `\">`\n- CSS injection attempts\n\nVerify the rendered email shows these as text or strips them safely.\n\n## 10) Prefer plain text when possible\nIf HTML isn’t required, plain-text emails are safest. If you need HTML, send **multipart** emails with both:\n- `text/plain`\n- `text/html`\n\nThat improves compatibility and provides a safe fallback.\n\n---\n\n## Practical safe rendering strategy\n1. Render the template with an engine that auto-escapes.\n2. Only pass prevalidated fields into the template.\n3. Escape all plain text variables.\n4. Sanitize any allowed rich HTML using a strict allowlist.\n5. Never render user HTML directly without sanitization.\n6. Validate URLs and other structured fields separately.\n\n---\n\n## Example approach in pseudocode\n```python\nsafe_name = escape(user.name)\nsafe_bio = sanitize_html(user.bio) # allowlist sanitizer\nsafe_link = validate_url(user.website)\n\nhtml = render_template(\"email.html\", {\n \"name\": safe_name,\n \"bio\": safe_bio,\n \"website\": safe_link,\n})\n```\n\n---\n\n## If you want, I can also show:\n- a **secure example** in Python/Jinja2, Node/Handlebars, or Ruby\n- how to safely allow **limited rich text**\n- a checklist for **multi-tenant email templates**"}}}

Prompt

How do I render HTML email templates safely for different users?

Technology / API Platforms1 observationLast seen Nov 22, 2026

Latest observation

Nov 22, 2026OpenAI APIWeb search: off

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:

  • https
  • mailto
  • maybe tel

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:

  • text/plain
  • text/html

That improves compatibility and provides a safe fallback.


Practical safe rendering strategy

  1. Render the template with an engine that auto-escapes.
  2. Only pass prevalidated fields into the template.
  3. Escape all plain text variables.
  4. Sanitize any allowed rich HTML using a strict allowlist.
  5. Never render user HTML directly without sanitization.
  6. 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

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.