2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/tls"
|
2015-10-15 21:17:52 +08:00
|
|
|
"encoding/base64"
|
2015-06-14 23:53:32 -08:00
|
|
|
"fmt"
|
2016-01-11 09:12:51 -06:00
|
|
|
l4g "github.com/alecthomas/log4go"
|
2015-06-14 23:53:32 -08:00
|
|
|
"github.com/mattermost/platform/model"
|
|
|
|
|
"net"
|
|
|
|
|
"net/mail"
|
|
|
|
|
"net/smtp"
|
2015-08-26 20:17:52 +02:00
|
|
|
"time"
|
2015-06-14 23:53:32 -08:00
|
|
|
)
|
|
|
|
|
|
2015-10-15 21:17:52 +08:00
|
|
|
func encodeRFC2047Word(s string) string {
|
|
|
|
|
// TODO: use `mime.BEncoding.Encode` instead when `go` >= 1.5
|
|
|
|
|
// return mime.BEncoding.Encode("utf-8", s)
|
|
|
|
|
dst := base64.StdEncoding.EncodeToString([]byte(s))
|
|
|
|
|
return "=?utf-8?b?" + dst + "?="
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-21 15:11:56 -07:00
|
|
|
func connectToSMTPServer(config *model.Config) (net.Conn, *model.AppError) {
|
2015-06-16 23:05:37 -08:00
|
|
|
var conn net.Conn
|
|
|
|
|
var err error
|
|
|
|
|
|
2015-09-21 17:34:13 -07:00
|
|
|
if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
|
2015-06-15 11:14:26 -04:00
|
|
|
tlsconfig := &tls.Config{
|
|
|
|
|
InsecureSkipVerify: true,
|
2015-09-21 15:11:56 -07:00
|
|
|
ServerName: config.EmailSettings.SMTPServer,
|
2015-06-15 11:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
2015-09-21 15:11:56 -07:00
|
|
|
conn, err = tls.Dial("tcp", config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort, tlsconfig)
|
2015-06-15 11:14:26 -04:00
|
|
|
if err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return nil, model.NewLocAppError("SendMail", "utils.mail.connect_smtp.open_tls.app_error", nil, err.Error())
|
2015-06-15 11:14:26 -04:00
|
|
|
}
|
2015-06-16 23:05:37 -08:00
|
|
|
} else {
|
2015-09-21 15:11:56 -07:00
|
|
|
conn, err = net.Dial("tcp", config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
|
2015-06-16 23:05:37 -08:00
|
|
|
if err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return nil, model.NewLocAppError("SendMail", "utils.mail.connect_smtp.open.app_error", nil, err.Error())
|
2015-06-16 23:05:37 -08:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-06-17 11:50:51 -04:00
|
|
|
return conn, nil
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-09-21 15:11:56 -07:00
|
|
|
func newSMTPClient(conn net.Conn, config *model.Config) (*smtp.Client, *model.AppError) {
|
|
|
|
|
c, err := smtp.NewClient(conn, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
|
2015-06-14 23:53:32 -08:00
|
|
|
if err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
l4g.Error(T("utils.mail.new_client.open.error"), err)
|
|
|
|
|
return nil, model.NewLocAppError("SendMail", "utils.mail.connect_smtp.open_tls.app_error", nil, err.Error())
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-06-15 11:14:26 -04:00
|
|
|
// GO does not support plain auth over a non encrypted connection.
|
|
|
|
|
// so if not tls then no auth
|
2015-09-21 15:11:56 -07:00
|
|
|
auth := smtp.PlainAuth("", config.EmailSettings.SMTPUsername, config.EmailSettings.SMTPPassword, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
|
2015-09-21 17:34:13 -07:00
|
|
|
if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
|
2015-06-15 11:14:26 -04:00
|
|
|
if err = c.Auth(auth); err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error())
|
2015-06-15 11:14:26 -04:00
|
|
|
}
|
2015-09-21 17:34:13 -07:00
|
|
|
} else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_STARTTLS {
|
2015-08-19 09:03:55 +02:00
|
|
|
tlsconfig := &tls.Config{
|
|
|
|
|
InsecureSkipVerify: true,
|
2015-09-21 15:11:56 -07:00
|
|
|
ServerName: config.EmailSettings.SMTPServer,
|
2015-08-19 09:03:55 +02:00
|
|
|
}
|
|
|
|
|
c.StartTLS(tlsconfig)
|
2015-08-23 22:13:07 -07:00
|
|
|
if err = c.Auth(auth); err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error())
|
2015-08-23 22:13:07 -07:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-06-17 11:50:51 -04:00
|
|
|
return c, nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-29 11:59:26 -07:00
|
|
|
func TestConnection(config *model.Config) {
|
|
|
|
|
if !config.EmailSettings.SendEmailNotifications {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err1 := connectToSMTPServer(config)
|
|
|
|
|
if err1 != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
l4g.Error(T("utils.mail.test.configured.error"), err1.Message, err1.DetailedError)
|
2015-09-29 11:59:26 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
|
|
c, err2 := newSMTPClient(conn, config)
|
|
|
|
|
if err2 != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
l4g.Error(T("utils.mail.test.configured.error"), err2.Message, err2.DetailedError)
|
2015-09-29 11:59:26 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer c.Quit()
|
|
|
|
|
defer c.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-17 11:50:51 -04:00
|
|
|
func SendMail(to, subject, body string) *model.AppError {
|
2015-09-21 15:11:56 -07:00
|
|
|
return SendMailUsingConfig(to, subject, body, Cfg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SendMailUsingConfig(to, subject, body string, config *model.Config) *model.AppError {
|
2015-09-29 11:59:26 -07:00
|
|
|
if !config.EmailSettings.SendEmailNotifications || len(config.EmailSettings.SMTPServer) == 0 {
|
2015-07-12 23:36:52 -08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-25 00:49:19 -03:00
|
|
|
l4g.Debug(T("utils.mail.send_mail.sending.debug"), to, subject)
|
2016-01-05 20:35:36 -06:00
|
|
|
|
2015-09-21 15:11:56 -07:00
|
|
|
fromMail := mail.Address{config.EmailSettings.FeedbackName, config.EmailSettings.FeedbackEmail}
|
2015-06-17 11:50:51 -04:00
|
|
|
toMail := mail.Address{"", to}
|
|
|
|
|
|
|
|
|
|
headers := make(map[string]string)
|
|
|
|
|
headers["From"] = fromMail.String()
|
|
|
|
|
headers["To"] = toMail.String()
|
2015-10-15 21:17:52 +08:00
|
|
|
headers["Subject"] = encodeRFC2047Word(subject)
|
2015-06-17 11:50:51 -04:00
|
|
|
headers["MIME-version"] = "1.0"
|
2015-10-15 21:17:52 +08:00
|
|
|
headers["Content-Type"] = "text/html; charset=\"utf-8\""
|
|
|
|
|
headers["Content-Transfer-Encoding"] = "8bit"
|
2015-08-27 15:02:50 +02:00
|
|
|
headers["Date"] = time.Now().Format(time.RFC1123Z)
|
2015-06-17 11:50:51 -04:00
|
|
|
|
|
|
|
|
message := ""
|
|
|
|
|
for k, v := range headers {
|
|
|
|
|
message += fmt.Sprintf("%s: %s\r\n", k, v)
|
|
|
|
|
}
|
|
|
|
|
message += "\r\n<html><body>" + body + "</body></html>"
|
|
|
|
|
|
2015-09-21 15:11:56 -07:00
|
|
|
conn, err1 := connectToSMTPServer(config)
|
2015-06-17 11:50:51 -04:00
|
|
|
if err1 != nil {
|
|
|
|
|
return err1
|
|
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
2015-09-21 15:11:56 -07:00
|
|
|
c, err2 := newSMTPClient(conn, config)
|
2015-06-17 11:50:51 -04:00
|
|
|
if err2 != nil {
|
|
|
|
|
return err2
|
|
|
|
|
}
|
|
|
|
|
defer c.Quit()
|
|
|
|
|
defer c.Close()
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-06-17 11:50:51 -04:00
|
|
|
if err := c.Mail(fromMail.Address); err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return model.NewLocAppError("SendMail", "utils.mail.send_mail.from_address.app_error", nil, err.Error())
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-06-17 11:50:51 -04:00
|
|
|
if err := c.Rcpt(toMail.Address); err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return model.NewLocAppError("SendMail", "utils.mail.send_mail.to_address.app_error", nil, err.Error())
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w, err := c.Data()
|
|
|
|
|
if err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return model.NewLocAppError("SendMail", "utils.mail.send_mail.msg_data.app_error", nil, err.Error())
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = w.Write([]byte(message))
|
|
|
|
|
if err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return model.NewLocAppError("SendMail", "utils.mail.send_mail.msg.app_error", nil, err.Error())
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = w.Close()
|
|
|
|
|
if err != nil {
|
2016-01-25 00:49:19 -03:00
|
|
|
return model.NewLocAppError("SendMail", "utils.mail.send_mail.close.app_error", nil, err.Error())
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|