From 5702e4bc2483ab976a1391f7517f5187527d9635 Mon Sep 17 00:00:00 2001 From: Guy Smoilov Date: Tue, 25 Dec 2018 17:08:00 +0200 Subject: 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 --- pkg/mailer/mailer.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'pkg/mailer/mailer.go') 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{}), -- cgit v1.2.3