diff --git a/pkg/services/notifications/smtp.go b/pkg/services/notifications/smtp.go index 4bc95742acb..9ac0ecff570 100644 --- a/pkg/services/notifications/smtp.go +++ b/pkg/services/notifications/smtp.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}) } diff --git a/pkg/services/notifications/smtp_test.go b/pkg/services/notifications/smtp_test.go index 78e9d76e454..8b4956bc3a9 100644 --- a/pkg/services/notifications/smtp_test.go +++ b/pkg/services/notifications/smtp_test.go @@ -31,7 +31,7 @@ func TestBuildMail(t *testing.T) { message := &Message{ To: []string{"to@address.com"}, - From: "from@address.com", + From: "Mr. Foo ", 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 ") + 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"))