Added configuration options for smtp

This commit is contained in:
Torkel Ödegaard 2015-06-08 17:56:56 +02:00
parent 42fc68baa5
commit db0c442eaf
9 changed files with 118 additions and 8 deletions

View File

@ -174,6 +174,20 @@ header_name = X-WEBAUTH-USER
header_property = username
auto_sign_up = true
#################################### SMTP / Emailing ##########################
[smtp]
enabled = false
host = localhost:25
user =
password =
cert_file =
key_file =
skip_verify = false
from_address = admin@grafana.localhost
[emails]
welcome_email_on_sign_up = false
#################################### Logging ##########################
[log]
# Either "console", "file", default is "console"

View File

@ -173,6 +173,20 @@
;header_property = username
;auto_sign_up = true
#################################### SMTP / Emailing ##########################
[smtp]
;enabled = false
;host = localhost:25
;user =
;password =
;cert_file =
;key_file =
;skip_verify = false
;from_address = admin@grafana.localhost
[emails]
;welcome_email_on_sign_up = false
#################################### Logging ##########################
[log]
# Either "console", "file", default is "console"

View File

@ -2,6 +2,7 @@ package api
import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/events"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
@ -24,6 +25,13 @@ func SignUp(c *middleware.Context, cmd m.CreateUserCommand) {
user := cmd.Result
bus.Publish(&events.UserSignedUp{
Id: user.Id,
Name: user.Name,
Email: user.Email,
Login: user.Login,
})
loginUserWithUser(&user, c)
c.JsonOK("User created and logged in")

View File

@ -70,6 +70,14 @@ type UserCreated struct {
Email string `json:"email"`
}
type UserSignedUp struct {
Timestamp time.Time `json:"timestamp"`
Id int64 `json:"id"`
Name string `json:"name"`
Login string `json:"login"`
Email string `json:"email"`
}
type UserUpdated struct {
Timestamp time.Time `json:"timestamp"`
Id int64 `json:"id"`

View File

@ -21,14 +21,6 @@ var mailQueue chan *Message
func initMailQueue() {
mailQueue = make(chan *Message, 10)
setting.Smtp = setting.SmtpSettings{
Host: "smtp.gmail.com:587",
User: "torkel.odegaard@gmail.com",
Password: "peslpwstnnloiksq",
FromAddress: "grafana@grafana.org",
}
go processMailQueue()
}

View File

@ -7,6 +7,8 @@ import (
"path/filepath"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/events"
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
@ -14,6 +16,7 @@ import (
var mailTemplates *template.Template
var tmplResetPassword = "reset_password.html"
var tmplWelcomeOnSignUp = "welcome_on_signup.html"
func Init() error {
initMailQueue()
@ -22,6 +25,8 @@ func Init() error {
bus.AddHandler("email", validateResetPasswordCode)
bus.AddHandler("email", sendEmailCommandHandler)
bus.AddEventListener(userSignedUpHandler)
mailTemplates = template.New("name")
mailTemplates.Funcs(template.FuncMap{
"Subject": subjectTemplateFunc,
@ -50,6 +55,10 @@ func subjectTemplateFunc(obj map[string]interface{}, value string) string {
}
func sendEmailCommandHandler(cmd *m.SendEmailCommand) error {
if !setting.Smtp.Enabled {
return errors.New("Grafana mailing/smtp options not configured, contact your Grafana admin")
}
var buffer bytes.Buffer
data := cmd.Data
if data == nil {
@ -98,3 +107,19 @@ func validateResetPasswordCode(query *m.ValidateResetPasswordCodeQuery) error {
query.Result = userQuery.Result
return nil
}
func userSignedUpHandler(evt *events.UserSignedUp) error {
log.Info("User signed up: %s, send_option: %s", evt.Email, setting.Smtp.SendWelcomeEmailOnSignUp)
if evt.Email == "" || !setting.Smtp.SendWelcomeEmailOnSignUp {
return nil
}
return sendEmailCommandHandler(&m.SendEmailCommand{
To: []string{evt.Email},
Template: tmplWelcomeOnSignUp,
Data: map[string]interface{}{
"Name": evt.Login,
},
})
}

View File

@ -409,6 +409,7 @@ func NewConfigContext(args *CommandLineArgs) {
GoogleAnalyticsId = analytics.Key("google_analytics_ua_id").String()
readSessionConfig()
readSmtpSettings()
}
func readSessionConfig() {

View File

@ -1,6 +1,7 @@
package setting
type SmtpSettings struct {
Enabled bool
Host string
User string
Password string
@ -8,4 +9,21 @@ type SmtpSettings struct {
KeyFile string
FromAddress string
SkipVerify bool
SendWelcomeEmailOnSignUp bool
}
func readSmtpSettings() {
sec := Cfg.Section("smtp")
Smtp.Enabled = sec.Key("enabled").MustBool(false)
Smtp.Host = sec.Key("host").String()
Smtp.User = sec.Key("user").String()
Smtp.Password = sec.Key("password").String()
Smtp.CertFile = sec.Key("cert_file").String()
Smtp.KeyFile = sec.Key("key_file").String()
Smtp.FromAddress = sec.Key("from_address").String()
Smtp.SkipVerify = sec.Key("skip_verify").MustBool(false)
emails := Cfg.Section("emails")
Smtp.SendWelcomeEmailOnSignUp = emails.Key("welcome_email_on_sign_up").MustBool(false)
}

View File

@ -0,0 +1,30 @@
{{Subject .Subject "Welcome to Grafana"}}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{{.Name}} Welcome to Grafana</title>
</head>
<body style="background:#eee;">
<div style="color:#333; font:12px/1.5 Tahoma,Arial,sans-serif;; text-shadow:1px 1px #fff; padding:0; margin:0;">
<div style="width:600px;margin:0 auto; padding:40px 0 20px;">
<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);">
<div style="padding: 20px 15px;">
<div style="padding:40px 15px;">
<div style="font-size:16px; padding-bottom:30px; font-weight:bold;">
Hi <span style="color: #00BFFF;">{{.Name}}</span>,
</div>
<div style="font-size:14px; padding:0 15px;">
</div>
</div>
</div>
</div>
<div style="color:#aaa;padding:10px;text-align:center;">
© 2014 <a style="color:#888;text-decoration:none;" target="_blank" href="http://grafana.org">Grafana</a>
</div>
</div>
</div>
</body>
</html>