fix(mail): preserve outer spacing in notification emails (#40104) (#40111)

Backport #40104 by @0xJacky

Remove the `body {margin: 0; padding: 0}` reset from
https://github.com/go-gitea/gitea/pull/38396, which left HTML mails
flush against the edge in Apple Mail and Thunderbird. The devtest
preview stops injecting its own padding, so it matches delivered mails.

These clients render the mail as a document and apply the default 8px
body margin again, as with GitHub and GitLab notifications. Webmail
clients replace `<body>` and keep their own inset.

Fixes: https://github.com/go-gitea/gitea/issues/40103
Assisted-by: Codex:GPT-6.1, Claude:Opus 5.5
Co-authored-by: Jacky <jacky-943572677@qq.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
authored and GitHub committed 2026-10-31 09:29:48 +00:00
1 parent 308dd93484
commit e395b4d9dc
2 files changed
+1 -4

No files matched your search

+1 -3
View File
@@ -49,13 +49,11 @@ func MailPreviewRender(ctx *context.Context) {
body := mailBody.String()
// emulate mail clients, which resolve "cid:" URIs to the mail's inline attachments
body = strings.ReplaceAll(body, `src="cid:`, `src="`+setting.AppSubURL+`/devtest/mail-preview-embed/`)
previewStyle := "body {padding: 12px 16px}"
// a page can force "color-scheme" on an embedded document but never "prefers-color-scheme"
if scheme := ctx.FormString("scheme"); scheme == "light" || scheme == "dark" {
body = mailDarkSchemeQuery.ReplaceAllString(body, util.Iif(scheme == "dark", "@media all", "@media not all"))
previewStyle += "\n:root {color-scheme: " + scheme + "}"
body = strings.Replace(body, "</head>", "<style>:root {color-scheme: "+scheme+"}</style></head>", 1)
}
body = strings.Replace(body, "</head>", "<style>"+previewStyle+"</style></head>", 1)
// fragment templates like "mail/base/head" would be sniffed as text/plain otherwise
ctx.Resp.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = ctx.Resp.Write([]byte(body))
-1
View File
@@ -5,7 +5,6 @@
<title>{{.}}</title>
<style>
body {margin: 0; padding: 0}
body, td {font-family: -apple-system, "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif}
a, a:visited {color: #0969da; text-decoration: none}
a:hover {text-decoration: underline}