Files
mattermost/utils/mail.go

175 lines
5.3 KiB
Go
Raw Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
package utils
import (
"crypto/tls"
2016-11-14 09:11:54 -03:00
"mime"
2015-06-14 23:53:32 -08:00
"net"
"net/mail"
"net/smtp"
2015-08-26 20:17:52 +02:00
"time"
"gopkg.in/gomail.v2"
l4g "github.com/alecthomas/log4go"
"github.com/mattermost/html2text"
"github.com/mattermost/platform/model"
"net/http"
2015-06-14 23:53:32 -08:00
)
2015-10-15 21:17:52 +08:00
func encodeRFC2047Word(s string) string {
2016-11-14 09:11:54 -03:00
return mime.BEncoding.Encode("utf-8", s)
2015-10-15 21:17:52 +08:00
}
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 {
tlsconfig := &tls.Config{
InsecureSkipVerify: *config.EmailSettings.SkipServerCertificateVerification,
2015-09-21 15:11:56 -07:00
ServerName: config.EmailSettings.SMTPServer,
}
2015-09-21 15:11:56 -07:00
conn, err = tls.Dial("tcp", config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort, tlsconfig)
if err != nil {
return nil, model.NewAppError("SendMail", "utils.mail.connect_smtp.open_tls.app_error", nil, err.Error(), http.StatusInternalServerError)
}
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 {
return nil, model.NewAppError("SendMail", "utils.mail.connect_smtp.open.app_error", nil, err.Error(), http.StatusInternalServerError)
2015-06-16 23:05:37 -08:00
}
2015-06-14 23:53:32 -08: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 {
l4g.Error(T("utils.mail.new_client.open.error"), err)
return nil, model.NewAppError("SendMail", "utils.mail.connect_smtp.open_tls.app_error", nil, err.Error(), http.StatusInternalServerError)
2015-06-14 23:53:32 -08:00
}
hostname := GetHostnameFromSiteURL(*config.ServiceSettings.SiteURL)
if hostname != "" {
err := c.Hello(hostname)
if err != nil {
l4g.Error(T("utils.mail.new_client.helo.error"), err)
return nil, model.NewAppError("SendMail", "utils.mail.connect_smtp.helo.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_STARTTLS {
tlsconfig := &tls.Config{
InsecureSkipVerify: *config.EmailSettings.SkipServerCertificateVerification,
2015-09-21 15:11:56 -07:00
ServerName: config.EmailSettings.SMTPServer,
}
c.StartTLS(tlsconfig)
}
if *config.EmailSettings.EnableSMTPAuth {
auth := smtp.PlainAuth("", config.EmailSettings.SMTPUsername, config.EmailSettings.SMTPPassword, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
if err = c.Auth(auth); err != nil {
return nil, model.NewAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error(), http.StatusInternalServerError)
}
2015-06-14 23:53:32 -08:00
}
return c, nil
}
func TestConnection(config *model.Config) {
if !config.EmailSettings.SendEmailNotifications {
return
}
conn, err1 := connectToSMTPServer(config)
if err1 != nil {
l4g.Error(T("utils.mail.test.configured.error"), T(err1.Message), err1.DetailedError)
return
}
defer conn.Close()
c, err2 := newSMTPClient(conn, config)
if err2 != nil {
l4g.Error(T("utils.mail.test.configured.error"), T(err2.Message), err2.DetailedError)
return
}
defer c.Quit()
defer c.Close()
}
func SendMail(to, subject, htmlBody string) *model.AppError {
return SendMailUsingConfig(to, subject, htmlBody, Cfg)
2015-09-21 15:11:56 -07:00
}
func SendMailUsingConfig(to, subject, htmlBody string, config *model.Config) *model.AppError {
if !config.EmailSettings.SendEmailNotifications || len(config.EmailSettings.SMTPServer) == 0 {
2015-07-12 23:36:52 -08:00
return nil
}
l4g.Debug(T("utils.mail.send_mail.sending.debug"), to, subject)
htmlMessage := "\r\n<html><body>" + htmlBody + "</body></html>"
fromMail := mail.Address{Name: config.EmailSettings.FeedbackName, Address: config.EmailSettings.FeedbackEmail}
txtBody, err := html2text.FromString(htmlBody)
if err != nil {
l4g.Warn(err)
txtBody = ""
}
m := gomail.NewMessage(gomail.SetCharset("UTF-8"))
m.SetHeaders(map[string][]string{
"From": {fromMail.String()},
"To": {to},
"Subject": {encodeRFC2047Word(subject)},
"Content-Transfer-Encoding": {"8bit"},
})
m.SetDateHeader("Date", time.Now())
m.SetBody("text/plain", txtBody)
m.AddAlternative("text/html", htmlMessage)
2015-09-21 15:11:56 -07:00
conn, err1 := connectToSMTPServer(config)
if err1 != nil {
return err1
}
defer conn.Close()
2015-09-21 15:11:56 -07:00
c, err2 := newSMTPClient(conn, config)
if err2 != nil {
return err2
}
defer c.Quit()
defer c.Close()
2015-06-14 23:53:32 -08:00
if err := c.Mail(fromMail.Address); err != nil {
return model.NewAppError("SendMail", "utils.mail.send_mail.from_address.app_error", nil, err.Error(), http.StatusInternalServerError)
2015-06-14 23:53:32 -08:00
}
if err := c.Rcpt(to); err != nil {
return model.NewAppError("SendMail", "utils.mail.send_mail.to_address.app_error", nil, err.Error(), http.StatusInternalServerError)
2015-06-14 23:53:32 -08:00
}
w, err := c.Data()
if err != nil {
return model.NewAppError("SendMail", "utils.mail.send_mail.msg_data.app_error", nil, err.Error(), http.StatusInternalServerError)
2015-06-14 23:53:32 -08:00
}
_, err = m.WriteTo(w)
2015-06-14 23:53:32 -08:00
if err != nil {
return model.NewAppError("SendMail", "utils.mail.send_mail.msg.app_error", nil, err.Error(), http.StatusInternalServerError)
2015-06-14 23:53:32 -08:00
}
err = w.Close()
if err != nil {
return model.NewAppError("SendMail", "utils.mail.send_mail.close.app_error", nil, err.Error(), http.StatusInternalServerError)
2015-06-14 23:53:32 -08:00
}
return nil
}