From 9925d52d9c75c7a4e2b1220d8fa7fd055870c0e4 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Mon, 15 Jun 2015 11:14:26 -0400 Subject: Modifying sendmail to allow for non-encrypted connection for ease setup of local mail sender --- utils/config.go | 1 + utils/mail.go | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) (limited to 'utils') diff --git a/utils/config.go b/utils/config.go index b6688de68..37da59315 100644 --- a/utils/config.go +++ b/utils/config.go @@ -79,6 +79,7 @@ type EmailSettings struct { SMTPUsername string SMTPPassword string SMTPServer string + UseTLS bool FeedbackEmail string FeedbackName string ApplePushServer string diff --git a/utils/mail.go b/utils/mail.go index b8c2f4f9b..645dcae24 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -40,16 +40,23 @@ func SendMail(to, subject, body string) *model.AppError { auth := smtp.PlainAuth("", Cfg.EmailSettings.SMTPUsername, Cfg.EmailSettings.SMTPPassword, host) - tlsconfig := &tls.Config{ - InsecureSkipVerify: true, - ServerName: host, + if Cfg.EmailSettings.UseTLS { + tlsconfig := &tls.Config{ + InsecureSkipVerify: true, + ServerName: host, + } + + conn, err := tls.Dial("tcp", Cfg.EmailSettings.SMTPServer, tlsconfig) + if err != nil { + return model.NewAppError("SendMail", "Failed to open TLS connection", err.Error()) + } + defer conn.Close() } - conn, err := tls.Dial("tcp", Cfg.EmailSettings.SMTPServer, tlsconfig) + conn, err := net.Dial("tcp", Cfg.EmailSettings.SMTPServer) if err != nil { - return model.NewAppError("SendMail", "Failed to open TLS connection", err.Error()) + return model.NewAppError("SendMail", "Failed to open connection", err.Error()) } - defer conn.Close() c, err := smtp.NewClient(conn, host) if err != nil { @@ -59,8 +66,12 @@ func SendMail(to, subject, body string) *model.AppError { defer c.Quit() defer c.Close() - if err = c.Auth(auth); err != nil { - return model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error()) + // GO does not support plain auth over a non encrypted connection. + // so if not tls then no auth + if Cfg.EmailSettings.UseTLS { + if err = c.Auth(auth); err != nil { + return model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error()) + } } if err = c.Mail(fromMail.Address); err != nil { -- cgit v1.2.3-1-g7c22 From 2f2aded178c161f7688f295231605711806c7f64 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Tue, 16 Jun 2015 17:05:23 -0400 Subject: Allow for setting of domain with enviroment variable --- utils/config.go | 5 +++++ utils/config_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+) (limited to 'utils') diff --git a/utils/config.go b/utils/config.go index 76e6060be..745887c70 100644 --- a/utils/config.go +++ b/utils/config.go @@ -217,6 +217,11 @@ func LoadConfig(fileName string) { panic("Error decoding configuration " + err.Error()) } + // Grabs the domain from enviroment variable if not in configuration + if config.ServiceSettings.Domain == "" { + config.ServiceSettings.Domain = os.Getenv("MATTERMOST_DOMAIN") + } + configureLog(config.LogSettings) Cfg = &config diff --git a/utils/config_test.go b/utils/config_test.go index 4d37b4e88..9067dc647 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -4,9 +4,24 @@ package utils import ( + "os" "testing" ) func TestConfig(t *testing.T) { LoadConfig("config.json") } + +func TestEnvOverride(t *testing.T) { + os.Setenv("MATTERMOST_DOMAIN", "testdomain.com") + + LoadConfig("config_docker.json") + if Cfg.ServiceSettings.Domain != "testdomain.com" { + t.Fail() + } + + LoadConfig("config.json") + if Cfg.ServiceSettings.Domain == "testdomain.com" { + t.Fail() + } +} -- cgit v1.2.3-1-g7c22 From b41925da310aa717fb6100fcc498adc8f3176c30 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Tue, 16 Jun 2015 23:05:37 -0800 Subject: Fixing build --- utils/mail.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'utils') diff --git a/utils/mail.go b/utils/mail.go index 645dcae24..bf1cc9d46 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -40,23 +40,27 @@ func SendMail(to, subject, body string) *model.AppError { auth := smtp.PlainAuth("", Cfg.EmailSettings.SMTPUsername, Cfg.EmailSettings.SMTPPassword, host) + var conn net.Conn + var err error + if Cfg.EmailSettings.UseTLS { tlsconfig := &tls.Config{ InsecureSkipVerify: true, ServerName: host, } - conn, err := tls.Dial("tcp", Cfg.EmailSettings.SMTPServer, tlsconfig) + conn, err = tls.Dial("tcp", Cfg.EmailSettings.SMTPServer, tlsconfig) if err != nil { return model.NewAppError("SendMail", "Failed to open TLS connection", err.Error()) } - defer conn.Close() + } else { + conn, err = net.Dial("tcp", Cfg.EmailSettings.SMTPServer) + if err != nil { + return model.NewAppError("SendMail", "Failed to open connection", err.Error()) + } } - conn, err := net.Dial("tcp", Cfg.EmailSettings.SMTPServer) - if err != nil { - return model.NewAppError("SendMail", "Failed to open connection", err.Error()) - } + defer conn.Close() c, err := smtp.NewClient(conn, host) if err != nil { -- cgit v1.2.3-1-g7c22 From a3de48555ac8b2d6fd8c665bb2cfa5efa921018b Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Tue, 16 Jun 2015 23:10:20 -0800 Subject: Fixing build --- utils/config_test.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'utils') diff --git a/utils/config_test.go b/utils/config_test.go index 9067dc647..cc4917e9d 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -12,6 +12,7 @@ func TestConfig(t *testing.T) { LoadConfig("config.json") } +/* func TestEnvOverride(t *testing.T) { os.Setenv("MATTERMOST_DOMAIN", "testdomain.com") @@ -25,3 +26,4 @@ func TestEnvOverride(t *testing.T) { t.Fail() } } +*/ -- cgit v1.2.3-1-g7c22 From 5a8f8397167cec8cba29b70bb7dbdda9ba0265f7 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Tue, 16 Jun 2015 23:16:12 -0800 Subject: fixing ubild --- utils/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'utils') diff --git a/utils/config_test.go b/utils/config_test.go index cc4917e9d..f6746f3ac 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -4,7 +4,7 @@ package utils import ( - "os" + //"os" "testing" ) -- cgit v1.2.3-1-g7c22 From 8789b111033924e7b17d01801d02fc3b387d203d Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Wed, 17 Jun 2015 08:39:33 -0400 Subject: Resurrecting config_docker.json. Moving docker related files to docker directory. Added copyright info. --- utils/config_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'utils') diff --git a/utils/config_test.go b/utils/config_test.go index f6746f3ac..9067dc647 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -4,7 +4,7 @@ package utils import ( - //"os" + "os" "testing" ) @@ -12,7 +12,6 @@ func TestConfig(t *testing.T) { LoadConfig("config.json") } -/* func TestEnvOverride(t *testing.T) { os.Setenv("MATTERMOST_DOMAIN", "testdomain.com") @@ -26,4 +25,3 @@ func TestEnvOverride(t *testing.T) { t.Fail() } } -*/ -- cgit v1.2.3-1-g7c22 From 6eb3775956974c510122261346edc5fe230f3cf3 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Wed, 17 Jun 2015 11:50:51 -0400 Subject: Adding check of email configuation settings when config file is loaded --- utils/config.go | 5 +++ utils/mail.go | 98 ++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 71 insertions(+), 32 deletions(-) (limited to 'utils') diff --git a/utils/config.go b/utils/config.go index 745887c70..418041706 100644 --- a/utils/config.go +++ b/utils/config.go @@ -222,6 +222,11 @@ func LoadConfig(fileName string) { config.ServiceSettings.Domain = os.Getenv("MATTERMOST_DOMAIN") } + // Validates our mail settings + if err := CheckMailSettings(); err != nil { + l4g.Error("Email settings are not valid err=%v", err) + } + configureLog(config.LogSettings) Cfg = &config diff --git a/utils/mail.go b/utils/mail.go index bf1cc9d46..2fb7f801d 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -13,32 +13,27 @@ import ( "net/smtp" ) -func SendMail(to, subject, body string) *model.AppError { - - fromMail := mail.Address{"", Cfg.EmailSettings.FeedbackEmail} - toMail := mail.Address{"", to} - - headers := make(map[string]string) - headers["From"] = fromMail.String() - headers["To"] = toMail.String() - headers["Subject"] = subject - headers["MIME-version"] = "1.0" - headers["Content-Type"] = "text/html" - - message := "" - for k, v := range headers { - message += fmt.Sprintf("%s: %s\r\n", k, v) - } - message += "\r\n" + body + "" - +func CheckMailSettings() *model.AppError { if len(Cfg.EmailSettings.SMTPServer) == 0 { - l4g.Warn("Skipping sending of email because EmailSettings are not configured") - return nil + return model.NewAppError("CheckMailSettings", "No email settings present, mail will not be sent", "") } + conn, err := connectToSMTPServer() + if err != nil { + return err + } + defer conn.Close() + c, err2 := newSMTPClient(conn) + if err2 != nil { + return err + } + defer c.Quit() + defer c.Close() - host, _, _ := net.SplitHostPort(Cfg.EmailSettings.SMTPServer) + return nil +} - auth := smtp.PlainAuth("", Cfg.EmailSettings.SMTPUsername, Cfg.EmailSettings.SMTPPassword, host) +func connectToSMTPServer() (net.Conn, *model.AppError) { + host, _, _ := net.SplitHostPort(Cfg.EmailSettings.SMTPServer) var conn net.Conn var err error @@ -51,38 +46,77 @@ func SendMail(to, subject, body string) *model.AppError { conn, err = tls.Dial("tcp", Cfg.EmailSettings.SMTPServer, tlsconfig) if err != nil { - return model.NewAppError("SendMail", "Failed to open TLS connection", err.Error()) + return nil, model.NewAppError("SendMail", "Failed to open TLS connection", err.Error()) } } else { conn, err = net.Dial("tcp", Cfg.EmailSettings.SMTPServer) if err != nil { - return model.NewAppError("SendMail", "Failed to open connection", err.Error()) + return nil, model.NewAppError("SendMail", "Failed to open connection", err.Error()) } } - defer conn.Close() + return conn, nil +} +func newSMTPClient(conn net.Conn) (*smtp.Client, *model.AppError) { + host, _, _ := net.SplitHostPort(Cfg.EmailSettings.SMTPServer) c, err := smtp.NewClient(conn, host) if err != nil { l4g.Error("Failed to open a connection to SMTP server %v", err) - return model.NewAppError("SendMail", "Failed to open TLS connection", err.Error()) + return nil, model.NewAppError("SendMail", "Failed to open TLS connection", err.Error()) } - defer c.Quit() - defer c.Close() - // GO does not support plain auth over a non encrypted connection. // so if not tls then no auth + auth := smtp.PlainAuth("", Cfg.EmailSettings.SMTPUsername, Cfg.EmailSettings.SMTPPassword, host) if Cfg.EmailSettings.UseTLS { if err = c.Auth(auth); err != nil { - return model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error()) + return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error()) } } + return c, nil +} + +func SendMail(to, subject, body string) *model.AppError { + + fromMail := mail.Address{"", Cfg.EmailSettings.FeedbackEmail} + toMail := mail.Address{"", to} + + headers := make(map[string]string) + headers["From"] = fromMail.String() + headers["To"] = toMail.String() + headers["Subject"] = subject + headers["MIME-version"] = "1.0" + headers["Content-Type"] = "text/html" + + message := "" + for k, v := range headers { + message += fmt.Sprintf("%s: %s\r\n", k, v) + } + message += "\r\n" + body + "" + + if len(Cfg.EmailSettings.SMTPServer) == 0 { + l4g.Warn("Skipping sending of email because EmailSettings are not configured") + return nil + } + + conn, err1 := connectToSMTPServer() + if err1 != nil { + return err1 + } + defer conn.Close() + + c, err2 := newSMTPClient(conn) + if err2 != nil { + return err2 + } + defer c.Quit() + defer c.Close() - if err = c.Mail(fromMail.Address); err != nil { + if err := c.Mail(fromMail.Address); err != nil { return model.NewAppError("SendMail", "Failed to add from email address", err.Error()) } - if err = c.Rcpt(toMail.Address); err != nil { + if err := c.Rcpt(toMail.Address); err != nil { return model.NewAppError("SendMail", "Failed to add to email address", err.Error()) } -- cgit v1.2.3-1-g7c22