aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorGarionion <github@entr0py.de>2021-12-08 08:34:23 +0100
committerGitHub <noreply@github.com>2021-12-08 15:34:23 +0800
commitb59875aa123f2cc3a5026d30ac557e99c05603a6 (patch)
tree7c8fcc51e1a80a87e035c5eded19e0da69429d43 /services
parent0ff18a808c7c14d42ea2325b5d9623f7a30d9107 (diff)
downloadgitea-b59875aa123f2cc3a5026d30ac557e99c05603a6.tar.gz
gitea-b59875aa123f2cc3a5026d30ac557e99c05603a6.zip
allways set a message-id on mails (#17900)
* allways set a message-id on mails * Add unit tests for mailer & Message-ID Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'services')
-rw-r--r--services/mailer/mailer.go16
-rw-r--r--services/mailer/mailer_test.go39
2 files changed, 55 insertions, 0 deletions
diff --git a/services/mailer/mailer.go b/services/mailer/mailer.go
index 4b1492df0b..eac2b15c3c 100644
--- a/services/mailer/mailer.go
+++ b/services/mailer/mailer.go
@@ -9,6 +9,7 @@ import (
"bytes"
"crypto/tls"
"fmt"
+ "hash/fnv"
"io"
"net"
"net/smtp"
@@ -67,6 +68,10 @@ func (m *Message) ToMessage() *gomail.Message {
msg.SetBody("text/plain", plainBody)
msg.AddAlternative("text/html", m.Body)
}
+
+ if len(msg.GetHeader("Message-ID")) == 0 {
+ msg.SetHeader("Message-ID", m.generateAutoMessageID())
+ }
return msg
}
@@ -75,6 +80,17 @@ func (m *Message) SetHeader(field string, value ...string) {
m.Headers[field] = value
}
+func (m *Message) generateAutoMessageID() string {
+ dateMs := m.Date.UnixNano() / 1e6
+ h := fnv.New64()
+ if len(m.To) > 0 {
+ _, _ = h.Write([]byte(m.To[0]))
+ }
+ _, _ = h.Write([]byte(m.Subject))
+ _, _ = h.Write([]byte(m.Body))
+ return fmt.Sprintf("<autogen-%d-%016x@%s>", dateMs, h.Sum64(), setting.Domain)
+}
+
// NewMessageFrom creates new mail message object with custom From header.
func NewMessageFrom(to []string, fromDisplayName, fromAddress, subject, body string) *Message {
log.Trace("NewMessageFrom (body):\n%s", body)
diff --git a/services/mailer/mailer_test.go b/services/mailer/mailer_test.go
new file mode 100644
index 0000000000..8505803d22
--- /dev/null
+++ b/services/mailer/mailer_test.go
@@ -0,0 +1,39 @@
+// Copyright 2021 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package mailer
+
+import (
+ "testing"
+ "time"
+
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestGenerateMessageID(t *testing.T) {
+ setting.LoadForTest(`
+[mailer]
+ENABLED = true
+FROM = test@domain.com
+`)
+ setting.NewServices()
+
+ date := time.Date(2000, 01, 02, 03, 04, 05, 06, time.UTC)
+ m := NewMessageFrom(nil, "display-name", "from-address", "subject", "body")
+ m.Date = date
+ gm := m.ToMessage()
+ assert.Equal(t, "<autogen-946782245000-41e8fc54a8ad3a3f@localhost>", gm.GetHeader("Message-ID")[0])
+
+ m = NewMessageFrom([]string{"a@b.com"}, "display-name", "from-address", "subject", "body")
+ m.Date = date
+ gm = m.ToMessage()
+ assert.Equal(t, "<autogen-946782245000-cc88ce3cfe9bd04f@localhost>", gm.GetHeader("Message-ID")[0])
+
+ m = NewMessageFrom([]string{"a@b.com"}, "display-name", "from-address", "subject", "body")
+ m.SetHeader("Message-ID", "<msg-d@domain.com>")
+ gm = m.ToMessage()
+ assert.Equal(t, "<msg-d@domain.com>", gm.GetHeader("Message-ID")[0])
+}