More work on email and notification infra #1456
This commit is contained in:
@@ -1,24 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
type SendEmailCommand struct {
|
|
||||||
To []string
|
|
||||||
From string
|
|
||||||
Subject string
|
|
||||||
Body string
|
|
||||||
Type string
|
|
||||||
Massive bool
|
|
||||||
Info string
|
|
||||||
}
|
|
||||||
|
|
||||||
// create mail content
|
|
||||||
func (m *SendEmailCommand) Content() string {
|
|
||||||
// set mail type
|
|
||||||
contentType := "text/plain; charset=UTF-8"
|
|
||||||
if m.Type == "html" {
|
|
||||||
contentType = "text/html; charset=UTF-8"
|
|
||||||
}
|
|
||||||
|
|
||||||
// create mail content
|
|
||||||
content := "From: " + m.From + "\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
|
|
||||||
return content
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package notifications
|
||||||
|
|
||||||
|
import (
|
||||||
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SendEmailCommand struct {
|
||||||
|
To []string
|
||||||
|
From string
|
||||||
|
Subject string
|
||||||
|
Body string
|
||||||
|
Type string
|
||||||
|
Massive bool
|
||||||
|
Info string
|
||||||
|
}
|
||||||
|
|
||||||
|
type SendResetPasswordEmailCommand struct {
|
||||||
|
Email string
|
||||||
|
}
|
||||||
|
|
||||||
|
// create mail content
|
||||||
|
func (m *SendEmailCommand) Content() string {
|
||||||
|
// set mail type
|
||||||
|
contentType := "text/plain; charset=UTF-8"
|
||||||
|
if m.Type == "html" {
|
||||||
|
contentType = "text/html; charset=UTF-8"
|
||||||
|
}
|
||||||
|
|
||||||
|
// create mail content
|
||||||
|
content := "From: " + m.From + "\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create html mail command
|
||||||
|
func NewSendEmailCommand(To []string, From, Subject, Body string) SendEmailCommand {
|
||||||
|
return SendEmailCommand{
|
||||||
|
To: To,
|
||||||
|
From: From,
|
||||||
|
Subject: Subject,
|
||||||
|
Body: Body,
|
||||||
|
Type: "html",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create New mail message use MailFrom and MailUser
|
||||||
|
func NewMailMessageFrom(To []string, from, subject, body string) SendEmailCommand {
|
||||||
|
return NewSendEmailCommand(To, from, subject, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create New mail message use MailFrom and MailUser
|
||||||
|
func NewMailMessage(To string, subject, body string) SendEmailCommand {
|
||||||
|
return NewMailMessageFrom([]string{To}, setting.Smtp.FromAddress, subject, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMailTmplData(u *m.User) map[interface{}]interface{} {
|
||||||
|
data := make(map[interface{}]interface{}, 10)
|
||||||
|
data["AppUrl"] = setting.AppUrl
|
||||||
|
data["BuildVersion"] = setting.BuildVersion
|
||||||
|
data["BuildStamp"] = setting.BuildStamp
|
||||||
|
data["BuildCommit"] = setting.BuildCommit
|
||||||
|
if u != nil {
|
||||||
|
data["User"] = u
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package notifications
|
||||||
|
|
||||||
|
import "github.com/grafana/grafana/pkg/bus"
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
bus.AddHandler("email", sendResetPasswordEmail)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendResetPasswordEmail(cmd *SendResetPasswordEmailCommand) error {
|
||||||
|
email := NewMailMessage("")
|
||||||
|
}
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package mailer
|
package mailer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -11,16 +15,16 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
"github.com/grafana/grafana/pkg/log"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/notifications"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mailQueue chan *m.SendEmailCommand
|
var mailQueue chan *notifications.SendEmailCommand
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
bus.AddHandler("email", handleEmailCommand)
|
bus.AddHandler("email", handleEmailCommand)
|
||||||
|
|
||||||
mailQueue = make(chan *m.SendEmailCommand, 10)
|
mailQueue = make(chan *notifications.SendEmailCommand, 10)
|
||||||
|
|
||||||
setting.Smtp = setting.SmtpSettings{
|
setting.Smtp = setting.SmtpSettings{
|
||||||
Host: "smtp.gmail.com:587",
|
Host: "smtp.gmail.com:587",
|
||||||
@@ -57,7 +61,7 @@ func encodeRFC2047(text string) string {
|
|||||||
return strings.Trim(addr.String(), " <>")
|
return strings.Trim(addr.String(), " <>")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleEmailCommand(cmd *m.SendEmailCommand) error {
|
func handleEmailCommand(cmd *notifications.SendEmailCommand) error {
|
||||||
log.Info("Sending on queue")
|
log.Info("Sending on queue")
|
||||||
mailQueue <- cmd
|
mailQueue <- cmd
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ const (
|
|||||||
var (
|
var (
|
||||||
// App settings.
|
// App settings.
|
||||||
Env string = DEV
|
Env string = DEV
|
||||||
AppName string
|
|
||||||
AppUrl string
|
AppUrl string
|
||||||
AppSubUrl string
|
AppSubUrl string
|
||||||
|
|
||||||
@@ -350,7 +349,6 @@ func NewConfigContext(args *CommandLineArgs) {
|
|||||||
setHomePath(args)
|
setHomePath(args)
|
||||||
loadConfiguration(args)
|
loadConfiguration(args)
|
||||||
|
|
||||||
AppName = Cfg.Section("").Key("app_name").MustString("Grafana")
|
|
||||||
Env = Cfg.Section("").Key("app_mode").MustString("development")
|
Env = Cfg.Section("").Key("app_mode").MustString("development")
|
||||||
|
|
||||||
server := Cfg.Section("server")
|
server := Cfg.Section("server")
|
||||||
|
|||||||
Reference in New Issue
Block a user