Skip to content

Commit db0c442

Browse files
committed
Added configuration options for smtp
1 parent 42fc68b commit db0c442

File tree

9 files changed

+118
-8
lines changed

9 files changed

+118
-8
lines changed

conf/defaults.ini

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ header_name = X-WEBAUTH-USER
174174
header_property = username
175175
auto_sign_up = true
176176

177+
#################################### SMTP / Emailing ##########################
178+
[smtp]
179+
enabled = false
180+
host = localhost:25
181+
user =
182+
password =
183+
cert_file =
184+
key_file =
185+
skip_verify = false
186+
from_address = admin@grafana.localhost
187+
188+
[emails]
189+
welcome_email_on_sign_up = false
190+
177191
#################################### Logging ##########################
178192
[log]
179193
# Either "console", "file", default is "console"

conf/sample.ini

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@
173173
;header_property = username
174174
;auto_sign_up = true
175175

176+
#################################### SMTP / Emailing ##########################
177+
[smtp]
178+
;enabled = false
179+
;host = localhost:25
180+
;user =
181+
;password =
182+
;cert_file =
183+
;key_file =
184+
;skip_verify = false
185+
;from_address = admin@grafana.localhost
186+
187+
[emails]
188+
;welcome_email_on_sign_up = false
189+
176190
#################################### Logging ##########################
177191
[log]
178192
# Either "console", "file", default is "console"

pkg/api/signup.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"github.com/grafana/grafana/pkg/bus"
5+
"github.com/grafana/grafana/pkg/events"
56
"github.com/grafana/grafana/pkg/metrics"
67
"github.com/grafana/grafana/pkg/middleware"
78
m "github.com/grafana/grafana/pkg/models"
@@ -24,6 +25,13 @@ func SignUp(c *middleware.Context, cmd m.CreateUserCommand) {
2425

2526
user := cmd.Result
2627

28+
bus.Publish(&events.UserSignedUp{
29+
Id: user.Id,
30+
Name: user.Name,
31+
Email: user.Email,
32+
Login: user.Login,
33+
})
34+
2735
loginUserWithUser(&user, c)
2836

2937
c.JsonOK("User created and logged in")

pkg/events/events.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ type UserCreated struct {
7070
Email string `json:"email"`
7171
}
7272

73+
type UserSignedUp struct {
74+
Timestamp time.Time `json:"timestamp"`
75+
Id int64 `json:"id"`
76+
Name string `json:"name"`
77+
Login string `json:"login"`
78+
Email string `json:"email"`
79+
}
80+
7381
type UserUpdated struct {
7482
Timestamp time.Time `json:"timestamp"`
7583
Id int64 `json:"id"`

pkg/services/notifications/mailer.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ var mailQueue chan *Message
2121

2222
func initMailQueue() {
2323
mailQueue = make(chan *Message, 10)
24-
25-
setting.Smtp = setting.SmtpSettings{
26-
Host: "smtp.gmail.com:587",
27-
User: "torkel.odegaard@gmail.com",
28-
Password: "peslpwstnnloiksq",
29-
FromAddress: "grafana@grafana.org",
30-
}
31-
3224
go processMailQueue()
3325
}
3426

pkg/services/notifications/notifications.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import (
77
"path/filepath"
88

99
"github.com/grafana/grafana/pkg/bus"
10+
"github.com/grafana/grafana/pkg/events"
11+
"github.com/grafana/grafana/pkg/log"
1012
m "github.com/grafana/grafana/pkg/models"
1113
"github.com/grafana/grafana/pkg/setting"
1214
"github.com/grafana/grafana/pkg/util"
1315
)
1416

1517
var mailTemplates *template.Template
1618
var tmplResetPassword = "reset_password.html"
19+
var tmplWelcomeOnSignUp = "welcome_on_signup.html"
1720

1821
func Init() error {
1922
initMailQueue()
@@ -22,6 +25,8 @@ func Init() error {
2225
bus.AddHandler("email", validateResetPasswordCode)
2326
bus.AddHandler("email", sendEmailCommandHandler)
2427

28+
bus.AddEventListener(userSignedUpHandler)
29+
2530
mailTemplates = template.New("name")
2631
mailTemplates.Funcs(template.FuncMap{
2732
"Subject": subjectTemplateFunc,
@@ -50,6 +55,10 @@ func subjectTemplateFunc(obj map[string]interface{}, value string) string {
5055
}
5156

5257
func sendEmailCommandHandler(cmd *m.SendEmailCommand) error {
58+
if !setting.Smtp.Enabled {
59+
return errors.New("Grafana mailing/smtp options not configured, contact your Grafana admin")
60+
}
61+
5362
var buffer bytes.Buffer
5463
data := cmd.Data
5564
if data == nil {
@@ -98,3 +107,19 @@ func validateResetPasswordCode(query *m.ValidateResetPasswordCodeQuery) error {
98107
query.Result = userQuery.Result
99108
return nil
100109
}
110+
111+
func userSignedUpHandler(evt *events.UserSignedUp) error {
112+
log.Info("User signed up: %s, send_option: %s", evt.Email, setting.Smtp.SendWelcomeEmailOnSignUp)
113+
114+
if evt.Email == "" || !setting.Smtp.SendWelcomeEmailOnSignUp {
115+
return nil
116+
}
117+
118+
return sendEmailCommandHandler(&m.SendEmailCommand{
119+
To: []string{evt.Email},
120+
Template: tmplWelcomeOnSignUp,
121+
Data: map[string]interface{}{
122+
"Name": evt.Login,
123+
},
124+
})
125+
}

pkg/setting/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ func NewConfigContext(args *CommandLineArgs) {
409409
GoogleAnalyticsId = analytics.Key("google_analytics_ua_id").String()
410410

411411
readSessionConfig()
412+
readSmtpSettings()
412413
}
413414

414415
func readSessionConfig() {

pkg/setting/setting_smtp.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
package setting
22

33
type SmtpSettings struct {
4+
Enabled bool
45
Host string
56
User string
67
Password string
78
CertFile string
89
KeyFile string
910
FromAddress string
1011
SkipVerify bool
12+
13+
SendWelcomeEmailOnSignUp bool
14+
}
15+
16+
func readSmtpSettings() {
17+
sec := Cfg.Section("smtp")
18+
Smtp.Enabled = sec.Key("enabled").MustBool(false)
19+
Smtp.Host = sec.Key("host").String()
20+
Smtp.User = sec.Key("user").String()
21+
Smtp.Password = sec.Key("password").String()
22+
Smtp.CertFile = sec.Key("cert_file").String()
23+
Smtp.KeyFile = sec.Key("key_file").String()
24+
Smtp.FromAddress = sec.Key("from_address").String()
25+
Smtp.SkipVerify = sec.Key("skip_verify").MustBool(false)
26+
27+
emails := Cfg.Section("emails")
28+
Smtp.SendWelcomeEmailOnSignUp = emails.Key("welcome_email_on_sign_up").MustBool(false)
1129
}

public/emails/welcome_on_signup.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{{Subject .Subject "Welcome to Grafana"}}
2+
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7+
<title>{{.Name}} Welcome to Grafana</title>
8+
</head>
9+
<body style="background:#eee;">
10+
<div style="color:#333; font:12px/1.5 Tahoma,Arial,sans-serif;; text-shadow:1px 1px #fff; padding:0; margin:0;">
11+
<div style="width:600px;margin:0 auto; padding:40px 0 20px;">
12+
<div style="border:1px solid #d9d9d9;border-radius:3px; background:#fff; box-shadow: 0px 2px 5px rgba(0, 0, 0,.05); -webkit-box-shadow: 0px 2px 5px rgba(0, 0, 0,.05);">
13+
<div style="padding: 20px 15px;">
14+
<div style="padding:40px 15px;">
15+
<div style="font-size:16px; padding-bottom:30px; font-weight:bold;">
16+
Hi <span style="color: #00BFFF;">{{.Name}}</span>,
17+
</div>
18+
<div style="font-size:14px; padding:0 15px;">
19+
20+
</div>
21+
</div>
22+
</div>
23+
</div>
24+
<div style="color:#aaa;padding:10px;text-align:center;">
25+
© 2014 <a style="color:#888;text-decoration:none;" target="_blank" href="http://grafana.org">Grafana</a>
26+
</div>
27+
</div>
28+
</div>
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)