2015-06-04 10:23:46 -05:00
|
|
|
// 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.
|
|
|
|
|
2015-06-08 09:51:25 -05:00
|
|
|
package notifications
|
2015-06-04 07:29:39 -05:00
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"bytes"
|
2015-06-04 07:29:39 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2016-10-03 02:38:03 -05:00
|
|
|
"html/template"
|
2015-06-04 07:29:39 -05:00
|
|
|
"net"
|
2016-10-20 08:06:59 -05:00
|
|
|
"strconv"
|
2015-06-04 07:29:39 -05:00
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-06-04 07:29:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-03-27 14:53:46 -05:00
|
|
|
gomail "gopkg.in/mail.v2"
|
2015-06-04 07:29:39 -05:00
|
|
|
)
|
|
|
|
|
2016-10-20 08:18:12 -05:00
|
|
|
func send(msg *Message) (int, error) {
|
|
|
|
dialer, err := createDialer()
|
2015-06-04 07:29:39 -05:00
|
|
|
if err != nil {
|
2016-10-20 08:18:12 -05:00
|
|
|
return 0, err
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
|
|
|
|
2016-10-20 08:18:12 -05:00
|
|
|
for _, address := range msg.To {
|
|
|
|
m := gomail.NewMessage()
|
|
|
|
m.SetHeader("From", msg.From)
|
|
|
|
m.SetHeader("To", address)
|
|
|
|
m.SetHeader("Subject", msg.Subject)
|
|
|
|
for _, file := range msg.EmbededFiles {
|
|
|
|
m.Embed(file)
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
|
|
|
|
2016-10-20 08:18:12 -05:00
|
|
|
m.SetBody("text/html", msg.Body)
|
2015-06-04 07:29:39 -05:00
|
|
|
|
2016-10-20 08:18:12 -05:00
|
|
|
if err := dialer.DialAndSend(m); err != nil {
|
|
|
|
return 0, err
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 08:18:12 -05:00
|
|
|
return len(msg.To), nil
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
|
|
|
|
2016-10-20 08:18:12 -05:00
|
|
|
func createDialer() (*gomail.Dialer, error) {
|
2016-10-20 08:06:59 -05:00
|
|
|
host, port, err := net.SplitHostPort(setting.Smtp.Host)
|
2015-06-04 07:29:39 -05:00
|
|
|
|
2016-10-20 08:06:59 -05:00
|
|
|
if err != nil {
|
2016-10-20 08:18:12 -05:00
|
|
|
return nil, err
|
2016-10-20 08:06:59 -05:00
|
|
|
}
|
|
|
|
iPort, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
2016-10-20 08:18:12 -05:00
|
|
|
return nil, err
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
|
|
|
|
2016-10-20 08:06:59 -05:00
|
|
|
tlsconfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: setting.Smtp.SkipVerify,
|
|
|
|
ServerName: host,
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.Smtp.CertFile != "" {
|
|
|
|
cert, err := tls.LoadX509KeyPair(setting.Smtp.CertFile, setting.Smtp.KeyFile)
|
2015-06-04 07:29:39 -05:00
|
|
|
if err != nil {
|
2016-12-15 01:27:21 -06:00
|
|
|
return nil, fmt.Errorf("Could not load cert or key file. error: %v", err)
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
2016-10-20 08:06:59 -05:00
|
|
|
tlsconfig.Certificates = []tls.Certificate{cert}
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
2016-10-20 08:06:59 -05:00
|
|
|
|
2016-11-02 16:11:59 -05:00
|
|
|
d := gomail.NewDialer(host, iPort, setting.Smtp.User, setting.Smtp.Password)
|
2016-10-20 08:06:59 -05:00
|
|
|
d.TLSConfig = tlsconfig
|
2017-09-22 16:29:56 -05:00
|
|
|
if setting.Smtp.EhloIdentity != "" {
|
|
|
|
d.LocalName = setting.Smtp.EhloIdentity
|
|
|
|
} else {
|
|
|
|
d.LocalName = setting.InstanceName
|
|
|
|
}
|
2016-10-20 08:18:12 -05:00
|
|
|
return d, nil
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
|
|
|
|
func buildEmailMessage(cmd *m.SendEmailCommand) (*Message, error) {
|
|
|
|
if !setting.Smtp.Enabled {
|
2017-04-25 06:16:37 -05:00
|
|
|
return nil, m.ErrSmtpNotEnabled
|
2016-10-03 02:38:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
var err error
|
|
|
|
|
|
|
|
data := cmd.Data
|
|
|
|
if data == nil {
|
|
|
|
data = make(map[string]interface{}, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTemplateData(data, nil)
|
|
|
|
err = mailTemplates.ExecuteTemplate(&buffer, cmd.Template, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-15 07:26:35 -06:00
|
|
|
subject := cmd.Subject
|
|
|
|
if cmd.Subject == "" {
|
|
|
|
var subjectText interface{}
|
|
|
|
subjectData := data["Subject"].(map[string]interface{})
|
|
|
|
subjectText, hasSubject := subjectData["value"]
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2016-12-15 07:26:35 -06:00
|
|
|
if !hasSubject {
|
2018-04-16 13:25:48 -05:00
|
|
|
return nil, fmt.Errorf("Missing subject in Template %s", cmd.Template)
|
2016-12-15 07:26:35 -06:00
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2016-12-15 07:26:35 -06:00
|
|
|
subjectTmpl, err := template.New("subject").Parse(subjectText.(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2016-12-15 07:26:35 -06:00
|
|
|
var subjectBuffer bytes.Buffer
|
|
|
|
err = subjectTmpl.ExecuteTemplate(&subjectBuffer, "subject", data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
subject = subjectBuffer.String()
|
2016-10-03 02:38:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Message{
|
2016-10-20 08:06:59 -05:00
|
|
|
To: cmd.To,
|
2017-02-14 14:51:41 -06:00
|
|
|
From: fmt.Sprintf("%s <%s>", setting.Smtp.FromName, setting.Smtp.FromAddress),
|
2016-12-15 07:26:35 -06:00
|
|
|
Subject: subject,
|
2016-10-20 08:06:59 -05:00
|
|
|
Body: buffer.String(),
|
|
|
|
EmbededFiles: cmd.EmbededFiles,
|
2016-10-03 02:38:03 -05:00
|
|
|
}, nil
|
|
|
|
}
|