diff --git a/app/notification_email.go b/app/notification_email.go index 2736ac7213..9690152a57 100644 --- a/app/notification_email.go +++ b/app/notification_email.go @@ -113,7 +113,7 @@ func (a *App) sendNotificationEmail(c request.CTX, notification *PostNotificatio return errors.Wrap(err, "unable to render the email notification template") } - templateString := "<%s@mattermost.com>" + templateString := "<%s@" + utils.GetHostnameFromSiteURL(a.GetSiteURL()) + ">" messageID := "" inReplyTo := "" references := "" diff --git a/shared/mail/mail.go b/shared/mail/mail.go index 03dbddc62a..e0ced44a5a 100644 --- a/shared/mail/mail.go +++ b/shared/mail/mail.go @@ -6,6 +6,7 @@ package mail import ( "context" "crypto/tls" + "fmt" "io" "mime" "net" @@ -17,6 +18,7 @@ import ( "github.com/pkg/errors" gomail "gopkg.in/mail.v2" + "github.com/mattermost/mattermost-server/v6/model" "github.com/mattermost/mattermost-server/v6/shared/mlog" ) @@ -282,10 +284,10 @@ func sendMailUsingConfigAdvanced(mail mailData, config *SMTPConfig) error { defer c.Quit() defer c.Close() - return SendMail(c, mail, time.Now()) + return sendMail(c, mail, time.Now(), config) } -func SendMail(c smtpClient, mail mailData, date time.Time) error { +func sendMail(c smtpClient, mail mailData, date time.Time, config *SMTPConfig) error { mlog.Debug("sending mail", mlog.String("to", mail.smtpTo), mlog.String("subject", mail.subject)) htmlMessage := mail.htmlBody @@ -315,6 +317,10 @@ func SendMail(c smtpClient, mail mailData, date time.Time) error { if mail.messageID != "" { headers["Message-ID"] = []string{mail.messageID} + } else { + randomStringLength := 16 + msgID := fmt.Sprintf("<%s-%d@%s>", model.NewRandomString(randomStringLength), time.Now().Unix(), config.Hostname) + headers["Message-ID"] = []string{msgID} } if mail.inReplyTo != "" { diff --git a/shared/mail/mail_test.go b/shared/mail/mail_test.go index ff5c0cd71c..202294d134 100644 --- a/shared/mail/mail_test.go +++ b/shared/mail/mail_test.go @@ -363,13 +363,13 @@ func TestSendMail(t *testing.T) { "\r\nMessage-ID: \r\n", "", }, - "doesn't add message-id header": { + "always adds message-id header": { mail.Address{}, "", "", "", + "\r\nMessage-ID: <", "", - "\r\nMessage-ID:", }, "adds in-reply-to header": { mail.Address{}, @@ -408,7 +408,8 @@ func TestSendMail(t *testing.T) { for testName, tc := range testCases { t.Run(testName, func(t *testing.T) { mail := mailData{"", "", mail.Address{}, "", tc.replyTo, "", "", nil, nil, tc.messageID, tc.inReplyTo, tc.references} - err = SendMail(mocm, mail, time.Now()) + cfg := getConfig() + err = sendMail(mocm, mail, time.Now(), cfg) require.NoError(t, err) if tc.contains != "" { require.Contains(t, string(mocm.data), tc.contains)