grafana/pkg/services/notifications/mailer.go

130 lines
2.8 KiB
Go
Raw Normal View History

// 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 notifications
2015-06-04 07:29:39 -05:00
import (
"bytes"
2015-06-04 07:29:39 -05:00
"crypto/tls"
"fmt"
"html/template"
2015-06-04 07:29:39 -05:00
"net"
"strconv"
2015-06-04 07:29:39 -05:00
m "github.com/grafana/grafana/pkg/models"
2015-06-04 07:29:39 -05:00
"github.com/grafana/grafana/pkg/setting"
gomail "gopkg.in/mail.v2"
2015-06-04 07:29:39 -05:00
)
func send(msg *Message) (int, error) {
dialer, err := createDialer()
2015-06-04 07:29:39 -05:00
if err != nil {
return 0, err
2015-06-04 07:29:39 -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
}
m.SetBody("text/html", msg.Body)
2015-06-04 07:29:39 -05:00
if err := dialer.DialAndSend(m); err != nil {
return 0, err
2015-06-04 07:29:39 -05:00
}
}
return len(msg.To), nil
2015-06-04 07:29:39 -05:00
}
func createDialer() (*gomail.Dialer, error) {
host, port, err := net.SplitHostPort(setting.Smtp.Host)
2015-06-04 07:29:39 -05:00
if err != nil {
return nil, err
}
iPort, err := strconv.Atoi(port)
if err != nil {
return nil, err
2015-06-04 07:29:39 -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 {
return nil, fmt.Errorf("Could not load cert or key file. error: %v", err)
2015-06-04 07:29:39 -05:00
}
tlsconfig.Certificates = []tls.Certificate{cert}
2015-06-04 07:29:39 -05:00
}
d := gomail.NewDialer(host, iPort, setting.Smtp.User, setting.Smtp.Password)
d.TLSConfig = tlsconfig
if setting.Smtp.EhloIdentity != "" {
d.LocalName = setting.Smtp.EhloIdentity
} else {
d.LocalName = setting.InstanceName
}
return d, nil
2015-06-04 07:29:39 -05:00
}
func buildEmailMessage(cmd *m.SendEmailCommand) (*Message, error) {
if !setting.Smtp.Enabled {
return nil, m.ErrSmtpNotEnabled
}
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
}
subject := cmd.Subject
if cmd.Subject == "" {
var subjectText interface{}
subjectData := data["Subject"].(map[string]interface{})
subjectText, hasSubject := subjectData["value"]
if !hasSubject {
return nil, fmt.Errorf("Missing subject in Template %s", cmd.Template)
}
subjectTmpl, err := template.New("subject").Parse(subjectText.(string))
if err != nil {
return nil, err
}
var subjectBuffer bytes.Buffer
err = subjectTmpl.ExecuteTemplate(&subjectBuffer, "subject", data)
if err != nil {
return nil, err
}
subject = subjectBuffer.String()
}
return &Message{
To: cmd.To,
From: fmt.Sprintf("%s <%s>", setting.Smtp.FromName, setting.Smtp.FromAddress),
Subject: subject,
Body: buffer.String(),
EmbededFiles: cmd.EmbededFiles,
}, nil
}