Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
42fc68ba
Commit
42fc68ba
authored
Jun 08, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplified emailing system and combined mailer and notifications packages
parent
c8bc0b3b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
54 additions
and
66 deletions
+54
-66
main.go
+0
-2
pkg/models/emails.go
+5
-23
pkg/services/notifications/email.go
+13
-9
pkg/services/notifications/mailer.go
+7
-13
pkg/services/notifications/notifications.go
+23
-12
pkg/services/notifications/notifications_test.go
+6
-7
No files found.
main.go
View file @
42fc68ba
...
...
@@ -15,7 +15,6 @@ import (
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/eventpublisher"
"github.com/grafana/grafana/pkg/services/mailer"
"github.com/grafana/grafana/pkg/services/notifications"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/services/sqlstore"
...
...
@@ -58,7 +57,6 @@ func main() {
social
.
NewOAuthService
()
eventpublisher
.
Init
()
plugins
.
Init
()
mailer
.
Init
()
if
err
:=
notifications
.
Init
();
err
!=
nil
{
log
.
Fatal
(
3
,
"Notification service failed to initialize"
,
err
)
...
...
pkg/models/emails.go
View file @
42fc68ba
...
...
@@ -5,12 +5,11 @@ import "errors"
var
ErrInvalidEmailCode
=
errors
.
New
(
"Invalid or expired email code"
)
type
SendEmailCommand
struct
{
To
[]
string
From
string
Subject
string
Body
string
Massive
bool
Info
string
To
[]
string
Template
string
Data
map
[
string
]
interface
{}
Massive
bool
Info
string
}
type
SendResetPasswordEmailCommand
struct
{
...
...
@@ -21,20 +20,3 @@ type ValidateResetPasswordCodeQuery struct {
Code
string
Result
*
User
}
// create mail content
func
(
m
*
SendEmailCommand
)
Content
()
string
{
contentType
:=
"text/html; charset=UTF-8"
content
:=
"From: "
+
m
.
From
+
"
\r\n
Subject: "
+
m
.
Subject
+
"
\r\n
Content-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
,
}
}
pkg/services/notifications/email.go
View file @
42fc68ba
...
...
@@ -5,18 +5,23 @@ import (
"github.com/grafana/grafana/pkg/setting"
)
// Create New mail message use MailFrom and MailUser
func
newMailMessageFrom
(
To
[]
string
,
from
,
subject
,
body
string
)
m
.
SendEmailCommand
{
return
m
.
NewSendEmailCommand
(
To
,
from
,
subject
,
body
)
type
Message
struct
{
To
[]
string
From
string
Subject
string
Body
string
Massive
bool
Info
string
}
// Create New mail message use MailFrom and MailUser
func
newMailMessage
(
To
string
,
subject
,
body
string
)
m
.
SendEmailCommand
{
return
newMailMessageFrom
([]
string
{
To
},
setting
.
Smtp
.
FromAddress
,
subject
,
body
)
// create mail content
func
(
m
*
Message
)
Content
()
string
{
contentType
:=
"text/html; charset=UTF-8"
content
:=
"From: "
+
m
.
From
+
"
\r\n
Subject: "
+
m
.
Subject
+
"
\r\n
Content-Type: "
+
contentType
+
"
\r\n\r\n
"
+
m
.
Body
return
content
}
func
getMailTmplData
(
u
*
m
.
User
)
map
[
interface
{}]
interface
{}
{
data
:=
make
(
map
[
interface
{}]
interface
{},
10
)
func
setDefaultTemplateData
(
data
map
[
string
]
interface
{},
u
*
m
.
User
)
{
data
[
"AppUrl"
]
=
setting
.
AppUrl
data
[
"BuildVersion"
]
=
setting
.
BuildVersion
data
[
"BuildStamp"
]
=
setting
.
BuildStamp
...
...
@@ -25,5 +30,4 @@ func getMailTmplData(u *m.User) map[interface{}]interface{} {
if
u
!=
nil
{
data
[
"Name"
]
=
u
.
NameOrFallback
()
}
return
data
}
pkg/services/
mailer
/mailer.go
→
pkg/services/
notifications
/mailer.go
View file @
42fc68ba
...
...
@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package
mailer
package
notifications
import
(
"crypto/tls"
...
...
@@ -13,18 +13,14 @@ import (
"os"
"strings"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)
var
mailQueue
chan
*
m
.
SendEmailCommand
var
mailQueue
chan
*
Message
func
Init
()
{
bus
.
AddHandler
(
"email"
,
handleEmailCommand
)
mailQueue
=
make
(
chan
*
m
.
SendEmailCommand
,
10
)
func
initMailQueue
()
{
mailQueue
=
make
(
chan
*
Message
,
10
)
setting
.
Smtp
=
setting
.
SmtpSettings
{
Host
:
"smtp.gmail.com:587"
,
...
...
@@ -55,10 +51,8 @@ func processMailQueue() {
}
}
func
handleEmailCommand
(
cmd
*
m
.
SendEmailCommand
)
error
{
log
.
Info
(
"Sending on queue"
)
mailQueue
<-
cmd
return
nil
var
addToMailQueue
=
func
(
msg
*
Message
)
{
mailQueue
<-
msg
}
func
sendToSmtpServer
(
recipients
[]
string
,
msgContent
[]
byte
)
error
{
...
...
@@ -162,7 +156,7 @@ func sendToSmtpServer(recipients []string, msgContent []byte) error {
return
client
.
Quit
()
}
func
buildAndSend
(
msg
*
m
.
SendEmailCommand
)
(
int
,
error
)
{
func
buildAndSend
(
msg
*
Message
)
(
int
,
error
)
{
log
.
Trace
(
"Sending mails to: %s"
,
strings
.
Join
(
msg
.
To
,
"; "
))
// get message body
...
...
pkg/services/notifications/notifications.go
View file @
42fc68ba
...
...
@@ -16,8 +16,11 @@ var mailTemplates *template.Template
var
tmplResetPassword
=
"reset_password.html"
func
Init
()
error
{
initMailQueue
()
bus
.
AddHandler
(
"email"
,
sendResetPasswordEmail
)
bus
.
AddHandler
(
"email"
,
validateResetPasswordCode
)
bus
.
AddHandler
(
"email"
,
sendEmailCommandHandler
)
mailTemplates
=
template
.
New
(
"name"
)
mailTemplates
.
Funcs
(
template
.
FuncMap
{
...
...
@@ -41,26 +44,23 @@ func Init() error {
return
nil
}
var
dispatchMail
=
func
(
cmd
*
m
.
SendEmailCommand
)
error
{
return
bus
.
Dispatch
(
cmd
)
}
func
subjectTemplateFunc
(
obj
map
[
string
]
interface
{},
value
string
)
string
{
obj
[
"value"
]
=
value
return
""
}
func
send
ResetPasswordEmail
(
cmd
*
m
.
SendResetPasswor
dEmailCommand
)
error
{
func
send
EmailCommandHandler
(
cmd
*
m
.
Sen
dEmailCommand
)
error
{
var
buffer
bytes
.
Buffer
data
:=
cmd
.
Data
if
data
==
nil
{
data
=
make
(
map
[
string
]
interface
{},
10
)
}
var
data
=
getMailTmplData
(
cmd
.
User
)
code
:=
createUserEmailCode
(
cmd
.
User
,
nil
)
data
[
"Code"
]
=
code
mailTemplates
.
ExecuteTemplate
(
&
buffer
,
tmplResetPassword
,
data
)
setDefaultTemplateData
(
data
,
nil
)
mailTemplates
.
ExecuteTemplate
(
&
buffer
,
cmd
.
Template
,
data
)
dispatchMail
(
&
m
.
SendEmailCommand
{
To
:
[]
string
{
cmd
.
User
.
Email
}
,
addToMailQueue
(
&
Message
{
To
:
cmd
.
To
,
From
:
setting
.
Smtp
.
FromAddress
,
Subject
:
data
[
"Subject"
]
.
(
map
[
string
]
interface
{})[
"value"
]
.
(
string
),
Body
:
buffer
.
String
(),
...
...
@@ -69,6 +69,17 @@ func sendResetPasswordEmail(cmd *m.SendResetPasswordEmailCommand) error {
return
nil
}
func
sendResetPasswordEmail
(
cmd
*
m
.
SendResetPasswordEmailCommand
)
error
{
return
sendEmailCommandHandler
(
&
m
.
SendEmailCommand
{
To
:
[]
string
{
cmd
.
User
.
Email
},
Template
:
tmplResetPassword
,
Data
:
map
[
string
]
interface
{}{
"Code"
:
createUserEmailCode
(
cmd
.
User
,
nil
),
"Name"
:
cmd
.
User
.
NameOrFallback
(),
},
})
}
func
validateResetPasswordCode
(
query
*
m
.
ValidateResetPasswordCodeQuery
)
error
{
login
:=
getLoginForEmailCode
(
query
.
Code
)
if
login
==
""
{
...
...
pkg/services/notifications/notifications_test.go
View file @
42fc68ba
...
...
@@ -20,17 +20,16 @@ func TestNotifications(t *testing.T) {
err
:=
Init
()
So
(
err
,
ShouldBeNil
)
var
sentMail
*
m
.
SendEmailCommand
dispatchMail
=
func
(
cmd
*
m
.
SendEmailCommand
)
error
{
sentMail
=
cmd
return
nil
var
sentMsg
*
Message
addToMailQueue
=
func
(
msg
*
Message
)
{
sentMsg
=
msg
}
Convey
(
"When sending reset email password"
,
func
()
{
sendResetPasswordEmail
(
&
m
.
SendResetPasswordEmailCommand
{
User
:
&
m
.
User
{
Email
:
"asd@asd.com"
}})
So
(
sentM
ail
.
Body
,
ShouldContainSubstring
,
"body"
)
So
(
sentM
ail
.
Subject
,
ShouldEqual
,
"Reset your Grafana password"
)
So
(
sentM
ail
.
Body
,
ShouldNotContainSubstring
,
"Subject"
)
So
(
sentM
sg
.
Body
,
ShouldContainSubstring
,
"body"
)
So
(
sentM
sg
.
Subject
,
ShouldEqual
,
"Reset your Grafana password"
)
So
(
sentM
sg
.
Body
,
ShouldNotContainSubstring
,
"Subject"
)
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment