Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send mails as HTML as default. Setting for send as plain text. #1648

Merged
merged 4 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions conf/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 14 additions & 13 deletions modules/mailer/mailer.go
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
Expand All @@ -29,24 +30,24 @@ 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)
msg.SetHeader("To", to...)
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{
Expand Down
21 changes: 13 additions & 8 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't remove old configs, deprecate them...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added warning

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(),
Expand All @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to use if SEND_AS_PLAIN_TEXT isn't being set :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it 👍

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)
Expand Down