diff options
author | Guy Smoilov <guy.smoilov@gmail.com> | 2018-12-25 17:08:00 +0200 |
---|---|---|
committer | 无闻 <u@gogs.io> | 2018-12-25 10:08:00 -0500 |
commit | 5702e4bc2483ab976a1391f7517f5187527d9635 (patch) | |
tree | 5b7ceab6f81277525bd566186e6e48d5e40158fe /pkg/mailer/mailer.go | |
parent | 9b37b1569c2d466dc8293ed482c1549aa9a6d4a9 (diff) |
pkg/mailer: support plaintext alt for HTML emails (#5568)
* Added option to use plain text alt to HTML emails. Should make the messages friendlier for spam filters.
* Check that plaintext conversion worked before adding the HTML alt
* Add description of ADD_PLAIN_TEXT_ALT to app.ini
* Added comment clarifying html AddAlternative
Diffstat (limited to 'pkg/mailer/mailer.go')
-rw-r--r-- | pkg/mailer/mailer.go | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/pkg/mailer/mailer.go b/pkg/mailer/mailer.go index b3bc1009..2d6ce52e 100644 --- a/pkg/mailer/mailer.go +++ b/pkg/mailer/mailer.go @@ -39,16 +39,24 @@ func NewMessageFrom(to []string, from, subject, htmlBody string) *Message { contentType := "text/html" body := htmlBody - if setting.MailService.UsePlainText { + switchedToPlaintext := false + if setting.MailService.UsePlainText || setting.MailService.AddPlainTextAlt { plainBody, err := html2text.FromString(htmlBody) if err != nil { log.Error(2, "html2text.FromString: %v", err) } else { contentType = "text/plain" body = plainBody + switchedToPlaintext = true } } msg.SetBody(contentType, body) + if switchedToPlaintext && setting.MailService.AddPlainTextAlt && !setting.MailService.UsePlainText { + // The AddAlternative method name is confusing - adding html as an "alternative" will actually cause mail + // clients to show it as first priority, and the text "main body" is the 2nd priority fallback. + // See: https://godoc.org/gopkg.in/gomail.v2#Message.AddAlternative + msg.AddAlternative("text/html", htmlBody) + } return &Message{ Message: msg, confirmChan: make(chan struct{}), |