[MM-36872] add threading headers to notification emails (#18232)

* [MM-36872] Adding headers for mail threading

* MM-36872 Add message id and mail threading headers

* MM-36872 Add message id and mail threading headers

* MM-36872 Add msgId, inreplyto, references headers

* Fixing comment issues

* Applying gofmt lint fixes

* Applying gofmt lint fixes

* MM-36872: Updates required from merge.

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Martin Kraft <martin@upspin.org>
Этот коммит содержится в:
Collin Eng
2022-06-29 12:29:29 -07:00
коммит произвёл GitHub
родитель 7e4024665c
Коммит 83b4e7285e
10 изменённых файлов: 115 добавлений и 24 удалений

Просмотреть файл

@@ -52,6 +52,9 @@ type mailData struct {
htmlBody string
embeddedFiles map[string]io.Reader
mimeHeaders map[string]string
messageID string
inReplyTo string
references string
}
// smtpClient is implemented by an smtp.Client. See https://golang.org/pkg/net/smtp/#Client.
@@ -230,7 +233,7 @@ func TestConnection(config *SMTPConfig) error {
return nil
}
func SendMailWithEmbeddedFilesUsingConfig(to, subject, htmlBody string, embeddedFiles map[string]io.Reader, config *SMTPConfig, enableComplianceFeatures bool, ccMail string) error {
func SendMailWithEmbeddedFilesUsingConfig(to, subject, htmlBody string, embeddedFiles map[string]io.Reader, config *SMTPConfig, enableComplianceFeatures bool, messageID string, inReplyTo string, references string, ccMail string) error {
fromMail := mail.Address{Name: config.FeedbackName, Address: config.FeedbackEmail}
replyTo := mail.Address{Name: config.FeedbackName, Address: config.ReplyToAddress}
@@ -243,13 +246,16 @@ func SendMailWithEmbeddedFilesUsingConfig(to, subject, htmlBody string, embedded
subject: subject,
htmlBody: htmlBody,
embeddedFiles: embeddedFiles,
messageID: messageID,
inReplyTo: inReplyTo,
references: references,
}
return sendMailUsingConfigAdvanced(mail, config)
}
func SendMailUsingConfig(to, subject, htmlBody string, config *SMTPConfig, enableComplianceFeatures bool, ccMail string) error {
return SendMailWithEmbeddedFilesUsingConfig(to, subject, htmlBody, nil, config, enableComplianceFeatures, ccMail)
func SendMailUsingConfig(to, subject, htmlBody string, config *SMTPConfig, enableComplianceFeatures bool, messageID string, inReplyTo string, references string, ccMail string) error {
return SendMailWithEmbeddedFilesUsingConfig(to, subject, htmlBody, nil, config, enableComplianceFeatures, messageID, inReplyTo, references, ccMail)
}
// allows for sending an email with differing MIME/SMTP recipients
@@ -308,6 +314,18 @@ func SendMail(c smtpClient, mail mailData, date time.Time) error {
headers["CC"] = []string{mail.cc}
}
if mail.messageID != "" {
headers["Message-ID"] = []string{mail.messageID}
}
if mail.inReplyTo != "" {
headers["In-Reply-To"] = []string{mail.inReplyTo}
}
if mail.references != "" {
headers["References"] = []string{mail.references}
}
for k, v := range mail.mimeHeaders {
headers[k] = []string{encodeRFC2047Word(v)}
}

Просмотреть файл

@@ -125,7 +125,7 @@ func TestSendMailUsingConfig(t *testing.T) {
//Delete all the messages before check the sample email
DeleteMailBox(emailTo)
err2 := SendMailUsingConfig(emailTo, emailSubject, emailBody, cfg, true, emailCC)
err2 := SendMailUsingConfig(emailTo, emailSubject, emailBody, cfg, true, "", "", "", emailCC)
require.NoError(t, err2, "Should connect to the SMTP Server")
//Check if the email was send to the right email address
@@ -163,7 +163,7 @@ func TestSendMailWithEmbeddedFilesUsingConfig(t *testing.T) {
"test1.png": bytes.NewReader([]byte("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")),
"test2.png": bytes.NewReader([]byte("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")),
}
err2 := SendMailWithEmbeddedFilesUsingConfig(emailTo, emailSubject, emailBody, embeddedFiles, cfg, true, emailCC)
err2 := SendMailWithEmbeddedFilesUsingConfig(emailTo, emailSubject, emailBody, embeddedFiles, cfg, true, "", "", "", emailCC)
require.NoError(t, err2, "Should connect to the SMTP Server")
//Check if the email was send to the right email address
@@ -333,24 +333,82 @@ func TestSendMail(t *testing.T) {
testCases := map[string]struct {
replyTo mail.Address
messageID string
inReplyTo string
references string
contains string
notContains string
}{
"adds reply-to header": {
mail.Address{Address: "foo@test.com"},
"",
"",
"",
"\r\nReply-To: <foo@test.com>\r\n",
"",
},
"doesn't add reply-to header": {
mail.Address{},
"",
"",
"",
"",
"\r\nReply-To:",
},
"adds message-id header": {
mail.Address{},
"<abc123@mattermost.com>",
"",
"",
"\r\nMessage-ID: <abc123@mattermost.com>\r\n",
"",
},
"doesn't add message-id header": {
mail.Address{},
"",
"",
"",
"",
"\r\nMessage-ID:",
},
"adds in-reply-to header": {
mail.Address{},
"",
"<defg456@mattermost.com>",
"",
"\r\nIn-Reply-To: <defg456@mattermost.com>\r\n",
"",
},
"doesn't add in-reply-to header": {
mail.Address{},
"",
"",
"",
"",
"\r\nIn-Reply-To:",
},
"adds references header": {
mail.Address{},
"",
"",
"<ghi789@mattermost.com>",
"\r\nReferences: <ghi789@mattermost.com>\r\n",
"",
},
"doesn't add references header": {
mail.Address{},
"",
"",
"",
"",
"\r\nReferences:",
},
}
for testName, tc := range testCases {
t.Run(testName, func(t *testing.T) {
mail := mailData{"", "", mail.Address{}, "", tc.replyTo, "", "", nil, nil}
mail := mailData{"", "", mail.Address{}, "", tc.replyTo, "", "", nil, nil, tc.messageID, tc.inReplyTo, tc.references}
err = SendMail(mocm, mail, time.Now())
require.NoError(t, err)
if tc.contains != "" {