mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 18:32:23 +08:00
Notifications: Add Message-ID header to outgoing emails (#83752)
* Add Message-ID header to outgoing emails * change message-id format * parse adddresses with name
This commit is contained in:
@ -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"))
|
||||
|
Reference in New Issue
Block a user