diff options
-rw-r--r-- | conf/app.ini | 2 | ||||
-rw-r--r-- | pkg/mailer/mailer.go | 10 | ||||
-rw-r--r-- | pkg/setting/setting.go | 26 |
3 files changed, 25 insertions, 13 deletions
diff --git a/conf/app.ini b/conf/app.ini index ab149369..1e06edcd 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -250,6 +250,8 @@ USER = PASSWD = ; Use text/plain as format of content USE_PLAIN_TEXT = false +; If sending html emails, then also attach a plaintext alternative to the MIME message, to support older mail clients and make spam filters happier. +ADD_PLAIN_TEXT_ALT = false [cache] ; Either "memory", "redis", or "memcache", default is "memory" 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{}), diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index b65de982..f293c73a 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -861,6 +861,7 @@ type Mailer struct { UseCertificate bool CertFile, KeyFile string UsePlainText bool + AddPlainTextAlt bool } var ( @@ -876,18 +877,19 @@ func newMailService() { } MailService = &Mailer{ - QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100), - SubjectPrefix: sec.Key("SUBJECT_PREFIX").MustString("[" + AppName + "] "), - Host: sec.Key("HOST").String(), - User: sec.Key("USER").String(), - Passwd: sec.Key("PASSWD").String(), - DisableHelo: sec.Key("DISABLE_HELO").MustBool(), - HeloHostname: sec.Key("HELO_HOSTNAME").String(), - SkipVerify: sec.Key("SKIP_VERIFY").MustBool(), - UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(), - CertFile: sec.Key("CERT_FILE").String(), - KeyFile: sec.Key("KEY_FILE").String(), - UsePlainText: sec.Key("USE_PLAIN_TEXT").MustBool(), + QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100), + SubjectPrefix: sec.Key("SUBJECT_PREFIX").MustString("[" + AppName + "] "), + Host: sec.Key("HOST").String(), + User: sec.Key("USER").String(), + Passwd: sec.Key("PASSWD").String(), + DisableHelo: sec.Key("DISABLE_HELO").MustBool(), + HeloHostname: sec.Key("HELO_HOSTNAME").String(), + SkipVerify: sec.Key("SKIP_VERIFY").MustBool(), + UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(), + CertFile: sec.Key("CERT_FILE").String(), + KeyFile: sec.Key("KEY_FILE").String(), + UsePlainText: sec.Key("USE_PLAIN_TEXT").MustBool(), + AddPlainTextAlt: sec.Key("ADD_PLAIN_TEXT_ALT").MustBool(), } MailService.From = sec.Key("FROM").MustString(MailService.User) |