feat(email): HTML email support with Markdown conversion - #46642
Closed
swissly wants to merge 2 commits into
Closed
Conversation
- Convert Markdown bodies to styled HTML with inline CSS - multipart/alternative emails (plain text + HTML) - Config toggle: platforms.email.html_format (default: true) - Gmail/Outlook compatible inline styles - Graceful fallback: HTML conversion failure -> plain text only - Security: no bleach needed for self-to-self emails Closes NousResearch#11941
Open
1 task
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds optional HTML-formatted email sending with inline styles by converting the existing Markdown body to a styled HTML variant and attaching it alongside the plain-text email.
Changes:
- Introduces HTML email wrapper markup (header/footer) plus a tag-to-inline-style mapping.
- Adds
_style_html_email()to inject inline element styles via regex substitutions. - Adds
html_formatconfig (default enabled) and sends multipart plain + HTML (with Markdown conversion) when enabled.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+103
to
+110
| for tag, style in _HERMES_EMAIL_ELEMENT_STYLES: | ||
| # Skip tags that already have a style attribute to avoid duplication | ||
| # Use negative lookahead: only match <tag that is NOT followed by ...style= | ||
| html = re.sub( | ||
| rf'<{tag}(?!\s+style=)(\s|>)', | ||
| rf'<{tag} {style}\1', | ||
| html, | ||
| ) |
Comment on lines
+51
to
+60
| _HERMES_EMAIL_CSS = """\ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head><meta charset="utf-8"></head> | ||
| <body style="margin:0;padding:0;background:#f4f4f7;"> | ||
| <div style="max-width:680px;margin:0 auto;background:#ffffff; | ||
| font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif; | ||
| font-size:15px;line-height:1.6;color:#2d3748;padding:32px;"> | ||
|
|
||
| """ |
Comment on lines
+648
to
+654
| if self._html_format: | ||
| try: | ||
| import markdown as _md | ||
| html_body = _md.markdown( | ||
| body, | ||
| extensions=["tables", "fenced_code", "nl2br"], | ||
| ) |
Comment on lines
+644
to
+657
| # Plain text fallback | ||
| msg.attach(MIMEText(body, "plain", "utf-8")) | ||
|
|
||
| # HTML version with inline CSS (if enabled) | ||
| if self._html_format: | ||
| try: | ||
| import markdown as _md | ||
| html_body = _md.markdown( | ||
| body, | ||
| extensions=["tables", "fenced_code", "nl2br"], | ||
| ) | ||
| html_body = _style_html_email(html_body) | ||
| html_email = _HERMES_EMAIL_CSS + html_body + _HERMES_EMAIL_FOOTER | ||
| msg.attach(MIMEText(html_email, "html", "utf-8")) |
Comment on lines
+345
to
+352
| # SECURITY NOTE: The Markdown library passes through raw HTML by default. | ||
| # We intentionally do NOT sanitize with bleach/allowlists because: | ||
| # 1. Hermes generates its own email bodies (LLM output, not user input) | ||
| # 2. Emails are sent to the configured address (self-to-self) | ||
| # 3. The config toggle provides a kill-switch: set html_format: false | ||
| # If the threat model changes (e.g. forwarding untrusted content), | ||
| # add bleach.clean() here with an allowed-tags list. | ||
| self._html_format = extra.get("html_format", True) |
Collaborator
- Fix regex: check style= anywhere in tag with [^>]* lookahead - Rename _HERMES_EMAIL_CSS to _HTML_PREFIX (accurate naming) - Validate markdown dependency once in __init__, auto-disable if missing - Wrap plain+HTML in multipart/alternative container (RFC correct) - Security: add HTML escaping note in comment
Contributor
Author
Addressed Copilot Review Round 3Fixes Applied
Verification
|
Contributor
Author
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds optional HTML email formatting by converting Markdown bodies into styled HTML and attaching both plain-text and HTML parts.
Changes
multipart/alternativeemails (plain text + HTML)platforms.email.html_format: true(default: enabled)Configuration
Security Note
The Markdown library passes through raw HTML by default. We intentionally do NOT sanitize with bleach/allowlists because:
html_format: falseTesting
Related