Skip to content

Commit 3706d0d

Browse files
committed
More work on email and notification infra grafana#1456
1 parent 3f5ab18 commit 3706d0d

File tree

5 files changed

+85
-30
lines changed

5 files changed

+85
-30
lines changed

pkg/models/emails.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

pkg/notifications/email.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package notifications
2+
3+
import (
4+
m "github.com/grafana/grafana/pkg/models"
5+
"github.com/grafana/grafana/pkg/setting"
6+
)
7+
8+
type SendEmailCommand struct {
9+
To []string
10+
From string
11+
Subject string
12+
Body string
13+
Type string
14+
Massive bool
15+
Info string
16+
}
17+
18+
type SendResetPasswordEmailCommand struct {
19+
Email string
20+
}
21+
22+
// create mail content
23+
func (m *SendEmailCommand) Content() string {
24+
// set mail type
25+
contentType := "text/plain; charset=UTF-8"
26+
if m.Type == "html" {
27+
contentType = "text/html; charset=UTF-8"
28+
}
29+
30+
// create mail content
31+
content := "From: " + m.From + "\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
32+
return content
33+
}
34+
35+
// Create html mail command
36+
func NewSendEmailCommand(To []string, From, Subject, Body string) SendEmailCommand {
37+
return SendEmailCommand{
38+
To: To,
39+
From: From,
40+
Subject: Subject,
41+
Body: Body,
42+
Type: "html",
43+
}
44+
}
45+
46+
// Create New mail message use MailFrom and MailUser
47+
func NewMailMessageFrom(To []string, from, subject, body string) SendEmailCommand {
48+
return NewSendEmailCommand(To, from, subject, body)
49+
}
50+
51+
// Create New mail message use MailFrom and MailUser
52+
func NewMailMessage(To string, subject, body string) SendEmailCommand {
53+
return NewMailMessageFrom([]string{To}, setting.Smtp.FromAddress, subject, body)
54+
}
55+
56+
func GetMailTmplData(u *m.User) map[interface{}]interface{} {
57+
data := make(map[interface{}]interface{}, 10)
58+
data["AppUrl"] = setting.AppUrl
59+
data["BuildVersion"] = setting.BuildVersion
60+
data["BuildStamp"] = setting.BuildStamp
61+
data["BuildCommit"] = setting.BuildCommit
62+
if u != nil {
63+
data["User"] = u
64+
}
65+
return data
66+
}

pkg/notifications/notifications.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package notifications
2+
3+
import "github.com/grafana/grafana/pkg/bus"
4+
5+
func Init() {
6+
bus.AddHandler("email", sendResetPasswordEmail)
7+
}
8+
9+
func sendResetPasswordEmail(cmd *SendResetPasswordEmailCommand) error {
10+
email := NewMailMessage("")
11+
}

pkg/services/mailer/mailer.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
15
package mailer
26

37
import (
@@ -11,16 +15,16 @@ import (
1115

1216
"github.com/grafana/grafana/pkg/bus"
1317
"github.com/grafana/grafana/pkg/log"
14-
m "github.com/grafana/grafana/pkg/models"
18+
"github.com/grafana/grafana/pkg/notifications"
1519
"github.com/grafana/grafana/pkg/setting"
1620
)
1721

18-
var mailQueue chan *m.SendEmailCommand
22+
var mailQueue chan *notifications.SendEmailCommand
1923

2024
func Init() {
2125
bus.AddHandler("email", handleEmailCommand)
2226

23-
mailQueue = make(chan *m.SendEmailCommand, 10)
27+
mailQueue = make(chan *notifications.SendEmailCommand, 10)
2428

2529
setting.Smtp = setting.SmtpSettings{
2630
Host: "smtp.gmail.com:587",
@@ -57,7 +61,7 @@ func encodeRFC2047(text string) string {
5761
return strings.Trim(addr.String(), " <>")
5862
}
5963

60-
func handleEmailCommand(cmd *m.SendEmailCommand) error {
64+
func handleEmailCommand(cmd *notifications.SendEmailCommand) error {
6165
log.Info("Sending on queue")
6266
mailQueue <- cmd
6367
return nil

pkg/setting/setting.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const (
3838
var (
3939
// App settings.
4040
Env string = DEV
41-
AppName string
4241
AppUrl string
4342
AppSubUrl string
4443

@@ -350,7 +349,6 @@ func NewConfigContext(args *CommandLineArgs) {
350349
setHomePath(args)
351350
loadConfiguration(args)
352351

353-
AppName = Cfg.Section("").Key("app_name").MustString("Grafana")
354352
Env = Cfg.Section("").Key("app_mode").MustString("development")
355353

356354
server := Cfg.Section("server")

0 commit comments

Comments
 (0)