mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Added configuration options for smtp
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -409,6 +409,7 @@ func NewConfigContext(args *CommandLineArgs) {
|
||||
GoogleAnalyticsId = analytics.Key("google_analytics_ua_id").String()
|
||||
|
||||
readSessionConfig()
|
||||
readSmtpSettings()
|
||||
}
|
||||
|
||||
func readSessionConfig() {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user