From 7b1110f1f4650e632c8a5d02bb874596e5d53d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=96stanb=C3=A4ck?= Date: Fri, 28 Apr 2017 23:27:05 +0200 Subject: [PATCH 1/4] Send mails as HTML as default. Setting for send as plain text. * Add new option SendAsPlainText. remove EnableHTMLAlternative * Send HTML mails as default * Add html check if html2text should be performed --- conf/app.ini | 4 ++-- modules/mailer/mailer.go | 31 ++++++++++++++++++++----------- modules/setting/setting.go | 16 ++++++++-------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/conf/app.ini b/conf/app.ini index 29836748d60ad..07164c27197af 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -290,8 +290,8 @@ FROM = ; Mailer user name and password USER = PASSWD = -; Use text/html as alternative format of content -ENABLE_HTML_ALTERNATIVE = false +; Send mails as plain text +SEND_AS_PLAIN_TEXT = false ; Enable sendmail (override SMTP) USE_SENDMAIL = false ; Specifiy an alternative sendmail binary diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index faff15fadf6bf..97a1f4c393886 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2017 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -15,11 +16,11 @@ import ( "strings" "time" - "github.com/jaytaylor/html2text" - "gopkg.in/gomail.v2" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + + "github.com/jaytaylor/html2text" + "gopkg.in/gomail.v2" ) // Message mail body and log info @@ -29,8 +30,8 @@ type Message struct { } // NewMessageFrom creates new mail message object with custom From header. -func NewMessageFrom(to []string, from, subject, htmlBody string) *Message { - log.Trace("NewMessageFrom (htmlBody):\n%s", htmlBody) +func NewMessageFrom(to []string, from, subject, body string) *Message { + log.Trace("NewMessageFrom (body):\n%s", body) msg := gomail.NewMessage() msg.SetHeader("From", from) @@ -38,15 +39,23 @@ func NewMessageFrom(to []string, from, subject, htmlBody string) *Message { msg.SetHeader("Subject", subject) msg.SetDateHeader("Date", time.Now()) - body, err := html2text.FromString(htmlBody) + var plainBody string + var err error + if strings.Contains(body[:100], "") { + plainBody, err = html2text.FromString(body) + } else { + plainBody = body + err = nil + } + if err != nil { log.Error(4, "html2text.FromString: %v", err) - msg.SetBody("text/html", htmlBody) + msg.SetBody("text/html", body) + } else if setting.MailService.SendAsPlainText { + msg.SetBody("text/plain", plainBody) } else { - msg.SetBody("text/plain", body) - if setting.MailService.EnableHTMLAlternative { - msg.AddAlternative("text/html", htmlBody) - } + msg.SetBody("text/plain", plainBody) + msg.AddAlternative("text/html", body) } return &Message{ diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 67018cf839e5f..3269b51a16679 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1252,11 +1252,11 @@ func newSessionService() { // Mailer represents mail service. type Mailer struct { // Mailer - QueueLength int - Name string - From string - FromEmail string - EnableHTMLAlternative bool + QueueLength int + Name string + From string + FromEmail string + SendAsPlainText bool // SMTP sender Host string @@ -1285,9 +1285,9 @@ func newMailService() { } MailService = &Mailer{ - QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100), - Name: sec.Key("NAME").MustString(AppName), - EnableHTMLAlternative: sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(), + QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100), + Name: sec.Key("NAME").MustString(AppName), + SendAsPlainText: sec.Key("SEND_AS_PLAIN_TEXT").MustBool(false), Host: sec.Key("HOST").String(), User: sec.Key("USER").String(), From 25989d214d2d2b3add0f7294b1830227dffdf3ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=96stanb=C3=A4ck?= Date: Tue, 9 May 2017 13:06:26 +0200 Subject: [PATCH 2/4] Send only multipart or plain. Add deprication warning for ENABLE_HTML_ALTERNATIVE --- modules/mailer/mailer.go | 18 +++++------------- modules/setting/setting.go | 4 ++++ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index 97a1f4c393886..2026db3a80424 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -39,19 +39,11 @@ func NewMessageFrom(to []string, from, subject, body string) *Message { msg.SetHeader("Subject", subject) msg.SetDateHeader("Date", time.Now()) - var plainBody string - var err error - if strings.Contains(body[:100], "") { - plainBody, err = html2text.FromString(body) - } else { - plainBody = body - err = nil - } - - if err != nil { - log.Error(4, "html2text.FromString: %v", err) - msg.SetBody("text/html", body) - } else if setting.MailService.SendAsPlainText { + plainBody, _ := html2text.FromString(body) + if setting.MailService.SendAsPlainText { + if strings.Contains(body[:100], "") { + log.Warn("Mail contains HTML but configured to send as plain text.") + } msg.SetBody("text/plain", plainBody) } else { msg.SetBody("text/plain", plainBody) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 3269b51a16679..baadfcf56a942 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1304,6 +1304,10 @@ func newMailService() { } MailService.From = sec.Key("FROM").MustString(MailService.User) + if sec.HasKey("ENABLE_HTML_ALTERNATIVE") { + log.Warn("ENABLE_HTML_ALTERNATIVE is deprecated, use SEND_AS_PLAIN_TEXT") + } + parsed, err := mail.ParseAddress(MailService.From) if err != nil { log.Fatal(4, "Invalid mailer.FROM (%s): %v", MailService.From, err) From 12fd2dab48e82538dcfd3b6d88515472a77e0cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=96stanb=C3=A4ck?= Date: Wed, 10 May 2017 14:34:19 +0200 Subject: [PATCH 3/4] Still use ENABLE_HTML_ALTERNATIVE for backward compatibility --- modules/setting/setting.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index baadfcf56a942..63635636b283b 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1306,6 +1306,7 @@ func newMailService() { if sec.HasKey("ENABLE_HTML_ALTERNATIVE") { log.Warn("ENABLE_HTML_ALTERNATIVE is deprecated, use SEND_AS_PLAIN_TEXT") + MailService.SendAsPlainText = !sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(false) } parsed, err := mail.ParseAddress(MailService.From) From 094bd67c8cff4f154904d1d92a9d0fd6e94ab4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=96stanb=C3=A4ck?= Date: Tue, 6 Jun 2017 21:55:23 +0200 Subject: [PATCH 4/4] Changed to not ignore html2text errors --- modules/mailer/mailer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index 2026db3a80424..d154cb9c2d582 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -39,8 +39,8 @@ func NewMessageFrom(to []string, from, subject, body string) *Message { msg.SetHeader("Subject", subject) msg.SetDateHeader("Date", time.Now()) - plainBody, _ := html2text.FromString(body) - if setting.MailService.SendAsPlainText { + plainBody, err := html2text.FromString(body) + if err != nil || setting.MailService.SendAsPlainText { if strings.Contains(body[:100], "") { log.Warn("Mail contains HTML but configured to send as plain text.") }