From bbcd0648acdb64f09379f9b38350927c0d58bf89 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 3 Apr 2026 19:59:08 +0200 Subject: [PATCH] fix(mail): set RFC 5322 compliant Message-ID using public URL domain Fixes #2522 --- pkg/mail/send_mail.go | 9 ++++++++ pkg/mail/send_mail_test.go | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 pkg/mail/send_mail_test.go diff --git a/pkg/mail/send_mail.go b/pkg/mail/send_mail.go index c19bdd81f..0ff34704c 100644 --- a/pkg/mail/send_mail.go +++ b/pkg/mail/send_mail.go @@ -17,7 +17,9 @@ package mail import ( + "crypto/rand" "embed" + "encoding/hex" "fmt" "io" @@ -78,6 +80,13 @@ func SendTestMail(opts *Opts) error { func getMessage(opts *Opts) *mail.Msg { m := mail.NewMsg() m.SetUserAgent("Vikunja " + version.Version) + + // Set an RFC 5322 compliant Message-ID using the public URL domain + // instead of relying on os.Hostname() which is unreliable in containers. + randBytes := make([]byte, 16) + _, _ = rand.Read(randBytes) + messageID := hex.EncodeToString(randBytes) + "@" + GetMailDomain() + m.SetMessageIDWithValue(messageID) if opts.From == "" { opts.From = "Vikunja <" + config.MailerFromEmail.GetString() + ">" } diff --git a/pkg/mail/send_mail_test.go b/pkg/mail/send_mail_test.go new file mode 100644 index 000000000..f39f974c0 --- /dev/null +++ b/pkg/mail/send_mail_test.go @@ -0,0 +1,42 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package mail + +import ( + "testing" + + "code.vikunja.io/api/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestGetMessageSetsMessageID(t *testing.T) { + config.ServicePublicURL.Set("https://tasks.example.com/") + config.MailerFromEmail.Set("test@example.com") + + opts := &Opts{ + To: "recipient@example.com", + Subject: "Test", + Message: "Hello", + ContentType: ContentTypePlain, + } + + m := getMessage(opts) + + msgID := m.GetMessageID() + assert.NotEmpty(t, msgID) + assert.Contains(t, msgID, "@tasks.example.com>") +}