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
tokarchuk/v1.17
Jonas Östanbäck 8 years ago committed by Lunny Xiao
parent 295f560a12
commit d9a8eff2de
  1. 4
      conf/app.ini
  2. 27
      modules/mailer/mailer.go
  3. 9
      modules/setting/setting.go

4
conf/app.ini vendored

@ -290,8 +290,8 @@ FROM =
; Mailer user name and password ; Mailer user name and password
USER = USER =
PASSWD = PASSWD =
; Use text/html as alternative format of content ; Send mails as plain text
ENABLE_HTML_ALTERNATIVE = false SEND_AS_PLAIN_TEXT = false
; Enable sendmail (override SMTP) ; Enable sendmail (override SMTP)
USE_SENDMAIL = false USE_SENDMAIL = false
; Specifiy an alternative sendmail binary ; Specifiy an alternative sendmail binary

@ -1,4 +1,5 @@
// Copyright 2014 The Gogs Authors. All rights reserved. // 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 // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -15,11 +16,11 @@ import (
"strings" "strings"
"time" "time"
"github.com/jaytaylor/html2text"
"gopkg.in/gomail.v2"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"github.com/jaytaylor/html2text"
"gopkg.in/gomail.v2"
) )
// Message mail body and log info // Message mail body and log info
@ -29,8 +30,8 @@ type Message struct {
} }
// NewMessageFrom creates new mail message object with custom From header. // NewMessageFrom creates new mail message object with custom From header.
func NewMessageFrom(to []string, from, subject, htmlBody string) *Message { func NewMessageFrom(to []string, from, subject, body string) *Message {
log.Trace("NewMessageFrom (htmlBody):\n%s", htmlBody) log.Trace("NewMessageFrom (body):\n%s", body)
msg := gomail.NewMessage() msg := gomail.NewMessage()
msg.SetHeader("From", from) msg.SetHeader("From", from)
@ -38,15 +39,15 @@ func NewMessageFrom(to []string, from, subject, htmlBody string) *Message {
msg.SetHeader("Subject", subject) msg.SetHeader("Subject", subject)
msg.SetDateHeader("Date", time.Now()) msg.SetDateHeader("Date", time.Now())
body, err := html2text.FromString(htmlBody) plainBody, err := html2text.FromString(body)
if err != nil { if err != nil || setting.MailService.SendAsPlainText {
log.Error(4, "html2text.FromString: %v", err) if strings.Contains(body[:100], "<html>") {
msg.SetBody("text/html", htmlBody) log.Warn("Mail contains HTML but configured to send as plain text.")
} else {
msg.SetBody("text/plain", body)
if setting.MailService.EnableHTMLAlternative {
msg.AddAlternative("text/html", htmlBody)
} }
msg.SetBody("text/plain", plainBody)
} else {
msg.SetBody("text/plain", plainBody)
msg.AddAlternative("text/html", body)
} }
return &Message{ return &Message{

@ -1256,7 +1256,7 @@ type Mailer struct {
Name string Name string
From string From string
FromEmail string FromEmail string
EnableHTMLAlternative bool SendAsPlainText bool
// SMTP sender // SMTP sender
Host string Host string
@ -1287,7 +1287,7 @@ func newMailService() {
MailService = &Mailer{ MailService = &Mailer{
QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100), QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100),
Name: sec.Key("NAME").MustString(AppName), Name: sec.Key("NAME").MustString(AppName),
EnableHTMLAlternative: sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(), SendAsPlainText: sec.Key("SEND_AS_PLAIN_TEXT").MustBool(false),
Host: sec.Key("HOST").String(), Host: sec.Key("HOST").String(),
User: sec.Key("USER").String(), User: sec.Key("USER").String(),
@ -1304,6 +1304,11 @@ func newMailService() {
} }
MailService.From = sec.Key("FROM").MustString(MailService.User) 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) parsed, err := mail.ParseAddress(MailService.From)
if err != nil { if err != nil {
log.Fatal(4, "Invalid mailer.FROM (%s): %v", MailService.From, err) log.Fatal(4, "Invalid mailer.FROM (%s): %v", MailService.From, err)

Loading…
Cancel
Save