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"
|
2019-08-26 09:44:18 -05:00
|
|
|
"io"
|
2015-06-04 07:29:39 -05:00
|
|
|
"net"
|
2016-10-20 08:06:59 -05:00
|
|
|
"strconv"
|
2020-01-10 09:06:33 -06:00
|
|
|
"strings"
|
2015-06-04 07:29:39 -05:00
|
|
|
|
2019-08-26 09:44:18 -05:00
|
|
|
gomail "gopkg.in/mail.v2"
|
|
|
|
|
2019-05-15 05:20:17 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-06-04 07:29:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2019-05-15 05:20:17 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2015-06-04 07:29:39 -05:00
|
|
|
)
|
|
|
|
|
2018-04-30 09:21:04 -05:00
|
|
|
func (ns *NotificationService) send(msg *Message) (int, error) {
|
2020-01-10 09:06:33 -06:00
|
|
|
messages := []*Message{}
|
|
|
|
|
|
|
|
if msg.SingleEmail {
|
|
|
|
messages = append(messages, msg)
|
|
|
|
} else {
|
|
|
|
for _, address := range msg.To {
|
|
|
|
copy := *msg
|
|
|
|
copy.To = []string{address}
|
|
|
|
messages = append(messages, ©)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns.dialAndSend(messages...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *NotificationService) dialAndSend(messages ...*Message) (num int, err error) {
|
2018-04-30 09:21:04 -05:00
|
|
|
dialer, err := ns.createDialer()
|
2015-06-04 07:29:39 -05:00
|
|
|
if err != nil {
|
2020-01-10 09:06:33 -06:00
|
|
|
return
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
|
|
|
|
2020-01-10 09:06:33 -06:00
|
|
|
for _, msg := range messages {
|
2016-10-20 08:18:12 -05:00
|
|
|
m := gomail.NewMessage()
|
|
|
|
m.SetHeader("From", msg.From)
|
2020-01-10 09:06:33 -06:00
|
|
|
m.SetHeader("To", msg.To...)
|
2016-10-20 08:18:12 -05:00
|
|
|
m.SetHeader("Subject", msg.Subject)
|
2019-08-26 09:44:18 -05:00
|
|
|
|
|
|
|
ns.setFiles(m, msg)
|
|
|
|
|
|
|
|
for _, replyTo := range msg.ReplyTo {
|
|
|
|
m.SetAddressHeader("Reply-To", replyTo, "")
|
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
|
|
|
|
2020-01-10 09:06:33 -06:00
|
|
|
if e := dialer.DialAndSend(m); e != nil {
|
|
|
|
err = errutil.Wrapf(e, "Failed to send notification to email addresses: %s", strings.Join(msg.To, ";"))
|
2019-05-10 01:39:51 -05:00
|
|
|
continue
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
2019-05-10 01:39:51 -05:00
|
|
|
|
2019-05-15 05:20:17 -05:00
|
|
|
num++
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
|
|
|
|
2020-01-10 09:06:33 -06:00
|
|
|
return
|
2015-06-04 07:29:39 -05:00
|
|
|
}
|
|
|
|
|
2019-08-26 09:44:18 -05:00
|
|
|
// setFiles attaches files in various forms
|
|
|
|
func (ns *NotificationService) setFiles(
|
|
|
|
m *gomail.Message,
|
|
|
|
msg *Message,
|
|
|
|
) {
|
2020-06-01 10:11:25 -05:00
|
|
|
for _, file := range msg.EmbeddedFiles {
|
2019-08-26 09:44:18 -05:00
|
|
|
m.Embed(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range msg.AttachedFiles {
|
|
|
|
m.Attach(file.Name, gomail.SetCopyFunc(func(writer io.Writer) error {
|
|
|
|
_, err := writer.Write(file.Content)
|
|
|
|
return err
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 09:21:04 -05:00
|
|
|
func (ns *NotificationService) createDialer() (*gomail.Dialer, error) {
|
|
|
|
host, port, err := net.SplitHostPort(ns.Cfg.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{
|
2018-04-30 09:21:04 -05:00
|
|
|
InsecureSkipVerify: ns.Cfg.Smtp.SkipVerify,
|
2016-10-20 08:06:59 -05:00
|
|
|
ServerName: host,
|
|
|
|
}
|
|
|
|
|
2018-04-30 09:21:04 -05:00
|
|
|
if ns.Cfg.Smtp.CertFile != "" {
|
|
|
|
cert, err := tls.LoadX509KeyPair(ns.Cfg.Smtp.CertFile, ns.Cfg.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
|
|
|
|
2018-04-30 09:21:04 -05:00
|
|
|
d := gomail.NewDialer(host, iPort, ns.Cfg.Smtp.User, ns.Cfg.Smtp.Password)
|
2016-10-20 08:06:59 -05:00
|
|
|
d.TLSConfig = tlsconfig
|
2020-05-13 09:33:40 -05:00
|
|
|
d.StartTLSPolicy = getStartTLSPolicy(ns.Cfg.Smtp.StartTLSPolicy)
|
2018-04-30 09:21:04 -05:00
|
|
|
|
|
|
|
if ns.Cfg.Smtp.EhloIdentity != "" {
|
|
|
|
d.LocalName = ns.Cfg.Smtp.EhloIdentity
|
2017-09-22 16:29:56 -05:00
|
|
|
} 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
|
|
|
|
2020-05-13 09:33:40 -05:00
|
|
|
func getStartTLSPolicy(policy string) gomail.StartTLSPolicy {
|
|
|
|
switch policy {
|
|
|
|
case "NoStartTLS":
|
|
|
|
return -1
|
|
|
|
case "MandatoryStartTLS":
|
|
|
|
return 1
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 05:20:17 -05:00
|
|
|
func (ns *NotificationService) buildEmailMessage(cmd *models.SendEmailCommand) (*Message, error) {
|
2018-04-30 09:21:04 -05:00
|
|
|
if !ns.Cfg.Smtp.Enabled {
|
2019-05-15 05:20:17 -05:00
|
|
|
return nil, models.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{
|
2019-08-26 09:44:18 -05:00
|
|
|
To: cmd.To,
|
2020-01-10 09:06:33 -06:00
|
|
|
SingleEmail: cmd.SingleEmail,
|
2019-08-26 09:44:18 -05:00
|
|
|
From: fmt.Sprintf("%s <%s>", ns.Cfg.Smtp.FromName, ns.Cfg.Smtp.FromAddress),
|
|
|
|
Subject: subject,
|
|
|
|
Body: buffer.String(),
|
2020-06-01 10:11:25 -05:00
|
|
|
EmbeddedFiles: cmd.EmbeddedFiles,
|
2019-08-26 09:44:18 -05:00
|
|
|
AttachedFiles: buildAttachedFiles(cmd.AttachedFiles),
|
2016-10-03 02:38:03 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2019-08-26 09:44:18 -05:00
|
|
|
|
|
|
|
// buildAttachedFiles build attached files
|
|
|
|
func buildAttachedFiles(
|
|
|
|
attached []*models.SendEmailAttachFile,
|
|
|
|
) []*AttachedFile {
|
|
|
|
result := make([]*AttachedFile, 0)
|
|
|
|
|
|
|
|
for _, file := range attached {
|
|
|
|
result = append(result, &AttachedFile{
|
|
|
|
Name: file.Name,
|
|
|
|
Content: file.Content,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|