Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config: Can add static headers to email messages #79365

Merged
merged 22 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b61e9ef
Can add allowed custom headers to an email Message. WIP.
owensmallwood Dec 11, 2023
edf0908
adds slug as a custom email header to all outgoing emails
owensmallwood Dec 11, 2023
29921ea
Headers are static - declared as key/value pairs in config. All stati…
owensmallwood Dec 12, 2023
4f914da
updates comment
owensmallwood Dec 12, 2023
188e739
adds tests for parsing smtp static headers
owensmallwood Dec 12, 2023
174190d
updates test to assert static headers are included when building email
owensmallwood Dec 12, 2023
14eff96
updates test to use multiple static headers
owensmallwood Dec 12, 2023
5d28dbf
updates test names
owensmallwood Dec 12, 2023
bf0ed0b
Merge branch 'main' into owensmallwood/can-configure-email-custom-hea…
owensmallwood Dec 12, 2023
db6c199
fixes linting issue with error
owensmallwood Dec 12, 2023
17d47cf
ignore gocyclo for loading config
owensmallwood Dec 12, 2023
12096b7
updates email headers in tests to be formatted properly
owensmallwood Dec 13, 2023
15c6d92
add static headers first
owensmallwood Dec 13, 2023
aa44c21
updates tests to assert that regular headers like From cant be overwr…
owensmallwood Dec 13, 2023
fbe2153
ensures only the header is in a valid format for smtp and not the value
owensmallwood Dec 13, 2023
2073062
updates comment and error message wording
owensmallwood Dec 14, 2023
5469247
adds to docs and ini sample files
owensmallwood Dec 14, 2023
4885c0e
updates smtp.static_headers docs examples formatting
owensmallwood Dec 14, 2023
88ebd4f
removes lines commented with semi colons
owensmallwood Dec 14, 2023
5910e71
prettier:write
owensmallwood Dec 14, 2023
ea61552
Merge branch 'main' into owensmallwood/can-configure-email-custom-hea…
owensmallwood Dec 14, 2023
3c5d944
renames var
owensmallwood Dec 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/services/notifications/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Message struct {
ReplyTo []string
EmbeddedFiles []string
AttachedFiles []*AttachedFile
CustomHeaders map[string]string
}

func setDefaultTemplateData(cfg *setting.Cfg, data map[string]any, u *user.User) {
Expand Down
1 change: 1 addition & 0 deletions pkg/services/notifications/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type SendEmailCommand struct {
ReplyTo []string
EmbeddedFiles []string
AttachedFiles []*SendEmailAttachFile
CustomHeaders map[string]string
}

// SendEmailCommandSync is the command for sending emails synchronously
Expand Down
2 changes: 2 additions & 0 deletions pkg/services/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (ns *NotificationService) SendEmailCommandHandlerSync(ctx context.Context,
AttachedFiles: cmd.AttachedFiles,
Subject: cmd.Subject,
ReplyTo: cmd.ReplyTo,
CustomHeaders: map[string]string{"slug": ns.Cfg.Slug},
hairyhenderson marked this conversation as resolved.
Show resolved Hide resolved
})

if err != nil {
Expand All @@ -208,6 +209,7 @@ func (ns *NotificationService) SendEmailCommandHandlerSync(ctx context.Context,
}

func (ns *NotificationService) SendEmailCommandHandler(ctx context.Context, cmd *SendEmailCommand) error {
cmd.CustomHeaders = map[string]string{"slug": ns.Cfg.Slug}
message, err := ns.buildEmailMessage(cmd)

if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/services/notifications/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -65,6 +66,12 @@ func (sc *SmtpClient) buildEmail(msg *Message) *gomail.Message {
m.SetHeader("From", msg.From)
m.SetHeader("To", msg.To...)
m.SetHeader("Subject", msg.Subject)
// add any allowed custom headers to the email message
for h, val := range msg.CustomHeaders {
if slices.Contains(sc.cfg.AllowedCustomHeaders, h) {
m.SetHeader(h, val)
}
}
sc.setFiles(m, msg)
for _, replyTo := range msg.ReplyTo {
m.SetAddressHeader("Reply-To", replyTo, "")
Expand Down
32 changes: 20 additions & 12 deletions pkg/setting/setting_smtp.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package setting

import "github.com/grafana/grafana/pkg/util"
import (
"strings"

"github.com/grafana/grafana/pkg/util"
)

type SmtpSettings struct {
Enabled bool
Host string
User string
Password string
CertFile string
KeyFile string
FromAddress string
FromName string
EhloIdentity string
StartTLSPolicy string
SkipVerify bool
Enabled bool
Host string
User string
Password string
CertFile string
KeyFile string
FromAddress string
FromName string
EhloIdentity string
StartTLSPolicy string
SkipVerify bool
AllowedCustomHeaders []string

SendWelcomeEmailOnSignUp bool
TemplatesPatterns []string
Expand All @@ -34,6 +39,9 @@ func (cfg *Cfg) readSmtpSettings() {
cfg.Smtp.StartTLSPolicy = sec.Key("startTLS_policy").String()
cfg.Smtp.SkipVerify = sec.Key("skip_verify").MustBool(false)

customHeaders := sec.Key("custom_headers").MustString("")
cfg.Smtp.AllowedCustomHeaders = strings.Split(customHeaders, ",")

emails := cfg.Raw.Section("emails")
cfg.Smtp.SendWelcomeEmailOnSignUp = emails.Key("welcome_email_on_sign_up").MustBool(false)
cfg.Smtp.TemplatesPatterns = util.SplitString(emails.Key("templates_pattern").MustString("emails/*.html, emails/*.txt"))
Expand Down