diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index c43024da691..42ebda9ea44 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -7,6 +7,7 @@ var ErrInvalidEmailCode = errors.New("Invalid or expired email code") type SendEmailCommand struct { To []string Template string + Subject string Data map[string]interface{} Info string EmbededFiles []string diff --git a/pkg/services/alerting/notifiers/email.go b/pkg/services/alerting/notifiers/email.go index bb01ba8b6e1..8cd4273e6be 100644 --- a/pkg/services/alerting/notifiers/email.go +++ b/pkg/services/alerting/notifiers/email.go @@ -57,6 +57,7 @@ func (this *EmailNotifier) Notify(evalContext *alerting.EvalContext) error { cmd := &m.SendEmailCommandSync{ SendEmailCommand: m.SendEmailCommand{ + Subject: evalContext.GetNotificationTitle(), Data: map[string]interface{}{ "Title": evalContext.GetNotificationTitle(), "State": evalContext.Rule.State, diff --git a/pkg/services/notifications/mailer.go b/pkg/services/notifications/mailer.go index c2d2cd5beca..97bf75fdcb9 100644 --- a/pkg/services/notifications/mailer.go +++ b/pkg/services/notifications/mailer.go @@ -111,7 +111,6 @@ func buildEmailMessage(cmd *m.SendEmailCommand) (*Message, error) { var buffer bytes.Buffer var err error - var subjectText interface{} data := cmd.Data if data == nil { @@ -124,28 +123,34 @@ func buildEmailMessage(cmd *m.SendEmailCommand) (*Message, error) { return nil, err } - subjectData := data["Subject"].(map[string]interface{}) - subjectText, hasSubject := subjectData["value"] + subject := cmd.Subject + if cmd.Subject == "" { + var subjectText interface{} + subjectData := data["Subject"].(map[string]interface{}) + subjectText, hasSubject := subjectData["value"] - if !hasSubject { - return nil, errors.New(fmt.Sprintf("Missing subject in Template %s", cmd.Template)) - } + if !hasSubject { + return nil, errors.New(fmt.Sprintf("Missing subject in Template %s", cmd.Template)) + } - subjectTmpl, err := template.New("subject").Parse(subjectText.(string)) - if err != nil { - return nil, err - } + subjectTmpl, err := template.New("subject").Parse(subjectText.(string)) + if err != nil { + return nil, err + } - var subjectBuffer bytes.Buffer - err = subjectTmpl.ExecuteTemplate(&subjectBuffer, "subject", data) - if err != nil { - return nil, err + var subjectBuffer bytes.Buffer + err = subjectTmpl.ExecuteTemplate(&subjectBuffer, "subject", data) + if err != nil { + return nil, err + } + + subject = subjectBuffer.String() } return &Message{ To: cmd.To, From: setting.Smtp.FromAddress, - Subject: subjectBuffer.String(), + Subject: subject, Body: buffer.String(), EmbededFiles: cmd.EmbededFiles, }, nil diff --git a/pkg/services/notifications/notifications.go b/pkg/services/notifications/notifications.go index 85bda1643d1..c1ed9ac9e74 100644 --- a/pkg/services/notifications/notifications.go +++ b/pkg/services/notifications/notifications.go @@ -80,6 +80,7 @@ func sendEmailCommandHandlerSync(ctx context.Context, cmd *m.SendEmailCommandSyn Template: cmd.Template, To: cmd.To, EmbededFiles: cmd.EmbededFiles, + Subject: cmd.Subject, }) if err != nil {