diff options
author | Jonas Östanbäck <cez81@users.noreply.github.com> | 2017-06-07 03:14:31 +0200 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-06-07 09:14:31 +0800 |
commit | d9a8eff2def111d1b038cbceea0a6b3ed7d43300 (patch) | |
tree | 761cf438ce6357f7ed40513133026a60030de2d4 /modules | |
parent | 295f560a124690b47da2e56369645092f7310129 (diff) | |
download | gitea-d9a8eff2def111d1b038cbceea0a6b3ed7d43300.tar.gz gitea-d9a8eff2def111d1b038cbceea0a6b3ed7d43300.zip |
Send mails as HTML as default. Setting for send as plain text. (#1648)
* 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
* Send only multipart or plain. Add deprication warning for ENABLE_HTML_ALTERNATIVE
* Still use ENABLE_HTML_ALTERNATIVE for backward compatibility
* Changed to not ignore html2text errors
Diffstat (limited to 'modules')
-rw-r--r-- | modules/mailer/mailer.go | 27 | ||||
-rw-r--r-- | modules/setting/setting.go | 21 |
2 files changed, 27 insertions, 21 deletions
diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index faff15fadf..d154cb9c2d 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,15 @@ func NewMessageFrom(to []string, from, subject, htmlBody string) *Message { msg.SetHeader("Subject", subject) msg.SetDateHeader("Date", time.Now()) - body, err := html2text.FromString(htmlBody) - if err != nil { - log.Error(4, "html2text.FromString: %v", err) - msg.SetBody("text/html", htmlBody) - } else { - msg.SetBody("text/plain", body) - if setting.MailService.EnableHTMLAlternative { - msg.AddAlternative("text/html", htmlBody) + plainBody, err := html2text.FromString(body) + if err != nil || setting.MailService.SendAsPlainText { + if strings.Contains(body[:100], "<html>") { + log.Warn("Mail contains HTML but configured to send as plain text.") } + msg.SetBody("text/plain", plainBody) + } else { + 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 67018cf839..63635636b2 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(), @@ -1304,6 +1304,11 @@ 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") + MailService.SendAsPlainText = !sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(false) + } + parsed, err := mail.ParseAddress(MailService.From) if err != nil { log.Fatal(4, "Invalid mailer.FROM (%s): %v", MailService.From, err) |