summaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/config.go15
-rw-r--r--utils/config_test.go4
-rw-r--r--utils/lru.go8
-rw-r--r--utils/lru_test.go8
-rw-r--r--utils/mail.go98
5 files changed, 94 insertions, 39 deletions
diff --git a/utils/config.go b/utils/config.go
index 745887c70..6a7e4589c 100644
--- a/utils/config.go
+++ b/utils/config.go
@@ -6,6 +6,7 @@ package utils
import (
l4g "code.google.com/p/log4go"
"encoding/json"
+ "net/mail"
"os"
"path/filepath"
)
@@ -220,12 +221,26 @@ func LoadConfig(fileName string) {
// Grabs the domain from enviroment variable if not in configuration
if config.ServiceSettings.Domain == "" {
config.ServiceSettings.Domain = os.Getenv("MATTERMOST_DOMAIN")
+ // If the enviroment variable is not set, use a default
+ if config.ServiceSettings.Domain == "" {
+ config.ServiceSettings.Domain = "localhost"
+ }
+ }
+
+ // Check for a valid email for feedback, if not then do feedback@domain
+ if _, err := mail.ParseAddress(config.EmailSettings.FeedbackEmail); err != nil {
+ config.EmailSettings.FeedbackEmail = "feedback@" + config.ServiceSettings.Domain
}
configureLog(config.LogSettings)
Cfg = &config
SanitizeOptions = getSanitizeOptions()
+
+ // Validates our mail settings
+ if err := CheckMailSettings(); err != nil {
+ l4g.Error("Email settings are not valid err=%v", err)
+ }
}
func getSanitizeOptions() map[string]bool {
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()
}
}
-*/
diff --git a/utils/lru.go b/utils/lru.go
index 9e47c3de3..61a515e14 100644
--- a/utils/lru.go
+++ b/utils/lru.go
@@ -1,5 +1,9 @@
-// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
-// See License.txt for license information.
+// This files was copied/modified from https://github.com/hashicorp/golang-lru
+// which was (see below)
+
+// This package provides a simple LRU cache. It is based on the
+// LRU implementation in groupcache:
+// https://github.com/golang/groupcache/tree/master/lru
package utils
diff --git a/utils/lru_test.go b/utils/lru_test.go
index e26af032f..3255f5c1a 100644
--- a/utils/lru_test.go
+++ b/utils/lru_test.go
@@ -1,5 +1,9 @@
-// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
-// See License.txt for license information.
+// This files was copied/modified from https://github.com/hashicorp/golang-lru
+// which was (see below)
+
+// This package provides a simple LRU cache. It is based on the
+// LRU implementation in groupcache:
+// https://github.com/golang/groupcache/tree/master/lru
package utils
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<html><body>" + body + "</body></html>"
-
+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<html><body>" + body + "</body></html>"
+
+ 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())
}