diff options
author | Carlos Tadeu Panato Junior <ctadeu@gmail.com> | 2017-08-21 15:31:13 -0300 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2017-08-21 11:31:13 -0700 |
commit | 12a9180807f6e1e53023de001eb7f1e611026a91 (patch) | |
tree | 0d6028c41d4b874f90ce195787d085d0bb704a15 /utils | |
parent | 65ccd4afb20b6bec40b8561e0eb0865372371b17 (diff) | |
download | chat-12a9180807f6e1e53023de001eb7f1e611026a91.tar.gz chat-12a9180807f6e1e53023de001eb7f1e611026a91.tar.bz2 chat-12a9180807f6e1e53023de001eb7f1e611026a91.zip |
[PLT-1015] Make all Mattermost system emails Multi-Part, with both a HTML and Plain Text version. (#5698)
* Implementation to Make all Mattermost system emails Multi-Part, with both a HTML and Plain Text version
* update lib
* update code per review
* update to use the mattermost repo
Diffstat (limited to 'utils')
-rw-r--r-- | utils/mail.go | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/utils/mail.go b/utils/mail.go index 41011d67e..6e87e5731 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -5,14 +5,16 @@ package utils import ( "crypto/tls" - "fmt" "mime" "net" "net/mail" "net/smtp" "time" + "gopkg.in/gomail.v2" + l4g "github.com/alecthomas/log4go" + "github.com/mattermost/html2text" "github.com/mattermost/platform/model" ) @@ -99,34 +101,38 @@ func TestConnection(config *model.Config) { defer c.Close() } -func SendMail(to, subject, body string) *model.AppError { - return SendMailUsingConfig(to, subject, body, Cfg) +func SendMail(to, subject, htmlBody string) *model.AppError { + return SendMailUsingConfig(to, subject, htmlBody, Cfg) } -func SendMailUsingConfig(to, subject, body string, config *model.Config) *model.AppError { +func SendMailUsingConfig(to, subject, htmlBody string, config *model.Config) *model.AppError { if !config.EmailSettings.SendEmailNotifications || len(config.EmailSettings.SMTPServer) == 0 { return nil } l4g.Debug(T("utils.mail.send_mail.sending.debug"), to, subject) - fromMail := mail.Address{Name: config.EmailSettings.FeedbackName, Address: config.EmailSettings.FeedbackEmail} - toMail := mail.Address{Name: "", Address: to} + htmlMessage := "\r\n<html><body>" + htmlBody + "</body></html>" - headers := make(map[string]string) - headers["From"] = fromMail.String() - headers["To"] = toMail.String() - headers["Subject"] = encodeRFC2047Word(subject) - headers["MIME-version"] = "1.0" - headers["Content-Type"] = "text/html; charset=\"utf-8\"" - headers["Content-Transfer-Encoding"] = "8bit" - headers["Date"] = time.Now().Format(time.RFC1123Z) + fromMail := mail.Address{Name: config.EmailSettings.FeedbackName, Address: config.EmailSettings.FeedbackEmail} - message := "" - for k, v := range headers { - message += fmt.Sprintf("%s: %s\r\n", k, v) + txtBody, err := html2text.FromString(htmlBody) + if err != nil { + l4g.Warn(err) + txtBody = "" } - message += "\r\n<html><body>" + body + "</body></html>" + + 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) conn, err1 := connectToSMTPServer(config) if err1 != nil { @@ -145,7 +151,7 @@ func SendMailUsingConfig(to, subject, body string, config *model.Config) *model. return model.NewLocAppError("SendMail", "utils.mail.send_mail.from_address.app_error", nil, err.Error()) } - if err := c.Rcpt(toMail.Address); err != nil { + if err := c.Rcpt(to); err != nil { return model.NewLocAppError("SendMail", "utils.mail.send_mail.to_address.app_error", nil, err.Error()) } @@ -154,11 +160,10 @@ func SendMailUsingConfig(to, subject, body string, config *model.Config) *model. return model.NewLocAppError("SendMail", "utils.mail.send_mail.msg_data.app_error", nil, err.Error()) } - _, err = w.Write([]byte(message)) + _, err = m.WriteTo(w) if err != nil { return model.NewLocAppError("SendMail", "utils.mail.send_mail.msg.app_error", nil, err.Error()) } - err = w.Close() if err != nil { return model.NewLocAppError("SendMail", "utils.mail.send_mail.close.app_error", nil, err.Error()) |