Skip to content

Commit 42fc68b

Browse files
committed
Simplified emailing system and combined mailer and notifications packages
1 parent c8bc0b3 commit 42fc68b

File tree

6 files changed

+54
-66
lines changed

6 files changed

+54
-66
lines changed

main.go

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/grafana/grafana/pkg/metrics"
1616
"github.com/grafana/grafana/pkg/plugins"
1717
"github.com/grafana/grafana/pkg/services/eventpublisher"
18-
"github.com/grafana/grafana/pkg/services/mailer"
1918
"github.com/grafana/grafana/pkg/services/notifications"
2019
"github.com/grafana/grafana/pkg/services/search"
2120
"github.com/grafana/grafana/pkg/services/sqlstore"
@@ -58,7 +57,6 @@ func main() {
5857
social.NewOAuthService()
5958
eventpublisher.Init()
6059
plugins.Init()
61-
mailer.Init()
6260

6361
if err := notifications.Init(); err != nil {
6462
log.Fatal(3, "Notification service failed to initialize", err)

pkg/models/emails.go

+5-23
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import "errors"
55
var ErrInvalidEmailCode = errors.New("Invalid or expired email code")
66

77
type SendEmailCommand struct {
8-
To []string
9-
From string
10-
Subject string
11-
Body string
12-
Massive bool
13-
Info string
8+
To []string
9+
Template string
10+
Data map[string]interface{}
11+
Massive bool
12+
Info string
1413
}
1514

1615
type SendResetPasswordEmailCommand struct {
@@ -21,20 +20,3 @@ type ValidateResetPasswordCodeQuery struct {
2120
Code string
2221
Result *User
2322
}
24-
25-
// create mail content
26-
func (m *SendEmailCommand) Content() string {
27-
contentType := "text/html; charset=UTF-8"
28-
content := "From: " + m.From + "\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
29-
return content
30-
}
31-
32-
// Create html mail command
33-
func NewSendEmailCommand(To []string, From, Subject, Body string) SendEmailCommand {
34-
return SendEmailCommand{
35-
To: To,
36-
From: From,
37-
Subject: Subject,
38-
Body: Body,
39-
}
40-
}

pkg/services/notifications/email.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ import (
55
"github.com/grafana/grafana/pkg/setting"
66
)
77

8-
// Create New mail message use MailFrom and MailUser
9-
func newMailMessageFrom(To []string, from, subject, body string) m.SendEmailCommand {
10-
return m.NewSendEmailCommand(To, from, subject, body)
8+
type Message struct {
9+
To []string
10+
From string
11+
Subject string
12+
Body string
13+
Massive bool
14+
Info string
1115
}
1216

13-
// Create New mail message use MailFrom and MailUser
14-
func newMailMessage(To string, subject, body string) m.SendEmailCommand {
15-
return newMailMessageFrom([]string{To}, setting.Smtp.FromAddress, subject, body)
17+
// create mail content
18+
func (m *Message) Content() string {
19+
contentType := "text/html; charset=UTF-8"
20+
content := "From: " + m.From + "\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
21+
return content
1622
}
1723

18-
func getMailTmplData(u *m.User) map[interface{}]interface{} {
19-
data := make(map[interface{}]interface{}, 10)
24+
func setDefaultTemplateData(data map[string]interface{}, u *m.User) {
2025
data["AppUrl"] = setting.AppUrl
2126
data["BuildVersion"] = setting.BuildVersion
2227
data["BuildStamp"] = setting.BuildStamp
@@ -25,5 +30,4 @@ func getMailTmplData(u *m.User) map[interface{}]interface{} {
2530
if u != nil {
2631
data["Name"] = u.NameOrFallback()
2732
}
28-
return data
2933
}

pkg/services/mailer/mailer.go pkg/services/notifications/mailer.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5-
package mailer
5+
package notifications
66

77
import (
88
"crypto/tls"
@@ -13,18 +13,14 @@ import (
1313
"os"
1414
"strings"
1515

16-
"github.com/grafana/grafana/pkg/bus"
1716
"github.com/grafana/grafana/pkg/log"
18-
m "github.com/grafana/grafana/pkg/models"
1917
"github.com/grafana/grafana/pkg/setting"
2018
)
2119

22-
var mailQueue chan *m.SendEmailCommand
20+
var mailQueue chan *Message
2321

24-
func Init() {
25-
bus.AddHandler("email", handleEmailCommand)
26-
27-
mailQueue = make(chan *m.SendEmailCommand, 10)
22+
func initMailQueue() {
23+
mailQueue = make(chan *Message, 10)
2824

2925
setting.Smtp = setting.SmtpSettings{
3026
Host: "smtp.gmail.com:587",
@@ -55,10 +51,8 @@ func processMailQueue() {
5551
}
5652
}
5753

58-
func handleEmailCommand(cmd *m.SendEmailCommand) error {
59-
log.Info("Sending on queue")
60-
mailQueue <- cmd
61-
return nil
54+
var addToMailQueue = func(msg *Message) {
55+
mailQueue <- msg
6256
}
6357

6458
func sendToSmtpServer(recipients []string, msgContent []byte) error {
@@ -162,7 +156,7 @@ func sendToSmtpServer(recipients []string, msgContent []byte) error {
162156
return client.Quit()
163157
}
164158

165-
func buildAndSend(msg *m.SendEmailCommand) (int, error) {
159+
func buildAndSend(msg *Message) (int, error) {
166160
log.Trace("Sending mails to: %s", strings.Join(msg.To, "; "))
167161

168162
// get message body

pkg/services/notifications/notifications.go

+23-12
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ var mailTemplates *template.Template
1616
var tmplResetPassword = "reset_password.html"
1717

1818
func Init() error {
19+
initMailQueue()
20+
1921
bus.AddHandler("email", sendResetPasswordEmail)
2022
bus.AddHandler("email", validateResetPasswordCode)
23+
bus.AddHandler("email", sendEmailCommandHandler)
2124

2225
mailTemplates = template.New("name")
2326
mailTemplates.Funcs(template.FuncMap{
@@ -41,26 +44,23 @@ func Init() error {
4144
return nil
4245
}
4346

44-
var dispatchMail = func(cmd *m.SendEmailCommand) error {
45-
return bus.Dispatch(cmd)
46-
}
47-
4847
func subjectTemplateFunc(obj map[string]interface{}, value string) string {
4948
obj["value"] = value
5049
return ""
5150
}
5251

53-
func sendResetPasswordEmail(cmd *m.SendResetPasswordEmailCommand) error {
52+
func sendEmailCommandHandler(cmd *m.SendEmailCommand) error {
5453
var buffer bytes.Buffer
54+
data := cmd.Data
55+
if data == nil {
56+
data = make(map[string]interface{}, 10)
57+
}
5558

56-
var data = getMailTmplData(cmd.User)
57-
code := createUserEmailCode(cmd.User, nil)
58-
data["Code"] = code
59-
60-
mailTemplates.ExecuteTemplate(&buffer, tmplResetPassword, data)
59+
setDefaultTemplateData(data, nil)
60+
mailTemplates.ExecuteTemplate(&buffer, cmd.Template, data)
6161

62-
dispatchMail(&m.SendEmailCommand{
63-
To: []string{cmd.User.Email},
62+
addToMailQueue(&Message{
63+
To: cmd.To,
6464
From: setting.Smtp.FromAddress,
6565
Subject: data["Subject"].(map[string]interface{})["value"].(string),
6666
Body: buffer.String(),
@@ -69,6 +69,17 @@ func sendResetPasswordEmail(cmd *m.SendResetPasswordEmailCommand) error {
6969
return nil
7070
}
7171

72+
func sendResetPasswordEmail(cmd *m.SendResetPasswordEmailCommand) error {
73+
return sendEmailCommandHandler(&m.SendEmailCommand{
74+
To: []string{cmd.User.Email},
75+
Template: tmplResetPassword,
76+
Data: map[string]interface{}{
77+
"Code": createUserEmailCode(cmd.User, nil),
78+
"Name": cmd.User.NameOrFallback(),
79+
},
80+
})
81+
}
82+
7283
func validateResetPasswordCode(query *m.ValidateResetPasswordCodeQuery) error {
7384
login := getLoginForEmailCode(query.Code)
7485
if login == "" {

pkg/services/notifications/notifications_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ func TestNotifications(t *testing.T) {
2020
err := Init()
2121
So(err, ShouldBeNil)
2222

23-
var sentMail *m.SendEmailCommand
24-
dispatchMail = func(cmd *m.SendEmailCommand) error {
25-
sentMail = cmd
26-
return nil
23+
var sentMsg *Message
24+
addToMailQueue = func(msg *Message) {
25+
sentMsg = msg
2726
}
2827

2928
Convey("When sending reset email password", func() {
3029
sendResetPasswordEmail(&m.SendResetPasswordEmailCommand{User: &m.User{Email: "[email protected]"}})
31-
So(sentMail.Body, ShouldContainSubstring, "body")
32-
So(sentMail.Subject, ShouldEqual, "Reset your Grafana password")
33-
So(sentMail.Body, ShouldNotContainSubstring, "Subject")
30+
So(sentMsg.Body, ShouldContainSubstring, "body")
31+
So(sentMsg.Subject, ShouldEqual, "Reset your Grafana password")
32+
So(sentMsg.Body, ShouldNotContainSubstring, "Subject")
3433
})
3534
})
3635

0 commit comments

Comments
 (0)