Browse Source

Do not write HTML in text/plain mail part (#2954)

* Do not write HTML in text/plain mail part

Fixes #2928

* Pass text/plain first, text/html second

* Do not send plain/text email if html2text failed (untested)
pull/3137/head
Sandro Santilli 9 years ago committed by Unknwon
parent
commit
d35a1c30f4
  1. 14
      modules/mailer/mailer.go

14
modules/mailer/mailer.go

@ -18,6 +18,7 @@ import (
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
"github.com/jaytaylor/html2text"
) )
type Message struct { type Message struct {
@ -26,14 +27,21 @@ 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, body string) *Message { func NewMessageFrom(to []string, from, subject, htmlbody string) *Message {
msg := gomail.NewMessage() msg := gomail.NewMessage()
msg.SetHeader("From", from) msg.SetHeader("From", from)
msg.SetHeader("To", to...) msg.SetHeader("To", to...)
msg.SetHeader("Subject", subject) msg.SetHeader("Subject", subject)
msg.SetDateHeader("Date", time.Now()) msg.SetDateHeader("Date", time.Now())
msg.SetBody("text/plain", body) body, err := html2text.FromString(htmlbody)
msg.AddAlternative("text/html", body) if err != nil {
// TODO: report error ?
msg.SetBody("text/html", htmlbody)
} else {
msg.SetBody("text/plain", body)
// TODO: avoid this (use a configuration switch?)
msg.AddAlternative("text/html", htmlbody)
}
return &Message{ return &Message{
Message: msg, Message: msg,

Loading…
Cancel
Save