[PR #1620] [MERGED] fix(smtp): preserve <br> line breaks in HTML emails #1667

Closed
opened 2026-05-07 01:03:09 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/binwiederhier/ntfy/pull/1620
Author: @uzkikh
Created: 2/21/2026
Status: Merged
Merged: 2/21/2026
Merged by: @binwiederhier

Base: mainHead: fix/smtp-html-br-linebreaks


📝 Commits (1)

  • 2e49938 fix(smtp): preserve
    line breaks in HTML emails

📊 Changes

2 files changed (+41 additions, -4 deletions)

View changed files

📝 server/smtp_server.go (+4 -0)
📝 server/smtp_server_test.go (+37 -4)

📄 Description

Context

HTML-only emails (e.g. from Synology DSM 7.3 Task Scheduler) use <br> tags for line breaks. The existing implementation passed the raw HTML body to bluemonday with AddSpaceWhenStrippingTag(true), which replaces every tag including <br> with a space. This caused all notification content to appear on a single unreadable line.

The ntfy notification before the fix

Task Scheduler has completed a scheduled task. Task: daily-backup Start time: Mon, 01 Jan 2026 02:00:00 +0000 Current status: 0 (Normal) From MyNAS

The ntfy notification after the fix

Task Scheduler has completed a scheduled task.

Task: daily-backup
Start time: Mon, 01 Jan 2026 02:00:00 +0000
Current status: 0 (Normal)

From MyNAS

Steps to reproduce

# Start ntfy with SMTP enabled
ntfy serve --smtp-server-listen :25 --smtp-server-domain example.com

# Send an HTML-only email with <br> line breaks
swaks \
  --server 127.0.0.1 --port 25 \
  --from sender@example.com \
  --to ntfy-mytopic@example.com \
  --header "Content-Type: text/html; charset=utf-8" \
  --body "Line one.<BR>Line two.<BR><BR>Line three."

# Check the notification — all text appears on one line:
#    "Line one. Line two.  Line three."

Root cause

The readHTMLMailBody() function used bluemonday.StrictPolicy().AddSpaceWhenStrippingTag(true) which replaces all tags including <br> with spaces, discarding line break semantics.

Fix

Convert <br> tags to newlines before passing to bluemonday. The regex covers all variants: <br>, <BR>, <br/>, <BR />.

Tests

  • Updated TestSmtpBackend_HTMLOnly_FromDiskStation and TestSmtpBackend_HTMLEmail to reflect correct output
  • Added TestSmtpBackend_HTMLEmail_BrTagsPreserved covering the Synology Task Scheduler email format

Related to #690


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/binwiederhier/ntfy/pull/1620 **Author:** [@uzkikh](https://github.com/uzkikh) **Created:** 2/21/2026 **Status:** ✅ Merged **Merged:** 2/21/2026 **Merged by:** [@binwiederhier](https://github.com/binwiederhier) **Base:** `main` ← **Head:** `fix/smtp-html-br-linebreaks` --- ### 📝 Commits (1) - [`2e49938`](https://github.com/binwiederhier/ntfy/commit/2e499389fc7068d73b8249d97ee7e28c6b2a86c6) fix(smtp): preserve <br> line breaks in HTML emails ### 📊 Changes **2 files changed** (+41 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `server/smtp_server.go` (+4 -0) 📝 `server/smtp_server_test.go` (+37 -4) </details> ### 📄 Description ## Context HTML-only emails (e.g. from Synology DSM 7.3 Task Scheduler) use `<br>` tags for line breaks. The existing implementation passed the raw HTML body to bluemonday with `AddSpaceWhenStrippingTag(true)`, which replaces every tag including `<br>` with a space. This caused all notification content to appear on a single unreadable line. ## The ntfy notification before the fix Task Scheduler has completed a scheduled task. Task: daily-backup Start time: Mon, 01 Jan 2026 02:00:00 +0000 Current status: 0 (Normal) From MyNAS ## The ntfy notification after the fix Task Scheduler has completed a scheduled task. Task: daily-backup Start time: Mon, 01 Jan 2026 02:00:00 +0000 Current status: 0 (Normal) From MyNAS ## Steps to reproduce ```bash # Start ntfy with SMTP enabled ntfy serve --smtp-server-listen :25 --smtp-server-domain example.com # Send an HTML-only email with <br> line breaks swaks \ --server 127.0.0.1 --port 25 \ --from sender@example.com \ --to ntfy-mytopic@example.com \ --header "Content-Type: text/html; charset=utf-8" \ --body "Line one.<BR>Line two.<BR><BR>Line three." # Check the notification — all text appears on one line: # "Line one. Line two. Line three." ``` ## Root cause The `readHTMLMailBody()` function used `bluemonday.StrictPolicy().AddSpaceWhenStrippingTag(true)` which replaces all tags including `<br>` with spaces, discarding line break semantics. ## Fix Convert `<br>` tags to newlines before passing to bluemonday. The regex covers all variants: `<br>`, `<BR>`, `<br/>`, `<BR />`. ## Tests - Updated `TestSmtpBackend_HTMLOnly_FromDiskStation` and `TestSmtpBackend_HTMLEmail` to reflect correct output - Added `TestSmtpBackend_HTMLEmail_BrTagsPreserved` covering the Synology Task Scheduler email format Related to #690 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-07 01:03:09 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ntfy#1667
No description provided.