Notifications: Add Message-ID header to outgoing emails (#83752)

* Add Message-ID header to outgoing emails

* change message-id format

* parse adddresses with name
fix/92181^2
janlugt 10 months ago committed by GitHub
parent 634d590cf1
commit 9d6935388c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      pkg/services/notifications/smtp.go
  2. 5
      pkg/services/notifications/smtp_test.go

@ -8,10 +8,12 @@ import (
"fmt"
"io"
"net"
"net/mail"
"net/textproto"
"strconv"
"strings"
"github.com/google/uuid"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
@ -101,6 +103,15 @@ func (sc *SmtpClient) buildEmail(ctx context.Context, msg *Message) *gomail.Mess
m.SetHeader("To", msg.To...)
m.SetHeader("Subject", msg.Subject)
from, err := mail.ParseAddress(msg.From)
if err == nil {
at := strings.LastIndex(from.Address, "@")
if at >= 0 {
domain := from.Address[at+1:]
m.SetHeader("Message-ID", fmt.Sprintf("<%s@%s>", uuid.NewString(), domain))
}
}
if sc.cfg.EnableTracing {
otel.GetTextMapPropagator().Inject(ctx, gomailHeaderCarrier{m})
}

@ -31,7 +31,7 @@ func TestBuildMail(t *testing.T) {
message := &Message{
To: []string{"to@address.com"},
From: "from@address.com",
From: "Mr. Foo <from@address.com>",
Subject: "Some subject",
Body: map[string]string{
"text/html": "Some HTML body",
@ -52,7 +52,8 @@ func TestBuildMail(t *testing.T) {
require.NoError(t, err)
assert.Contains(t, buf.String(), "Foo-Header: foo_value")
assert.Contains(t, buf.String(), "From: from@address.com")
assert.Contains(t, buf.String(), "From: Mr. Foo <from@address.com>")
assert.Regexp(t, "Message-ID: <.*@address.com>", buf.String())
assert.Contains(t, buf.String(), "Some HTML body")
assert.Contains(t, buf.String(), "Some plain text body")
assert.Less(t, strings.Index(buf.String(), "Some plain text body"), strings.Index(buf.String(), "Some HTML body"))

Loading…
Cancel
Save