@ -1,4 +1,5 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// 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
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
@ -15,11 +16,11 @@ import (
"strings"
"strings"
"time"
"time"
"github.com/jaytaylor/html2text"
"gopkg.in/gomail.v2"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
"github.com/jaytaylor/html2text"
"gopkg.in/gomail.v2"
)
)
// Message mail body and log info
// Message mail body and log info
@ -29,8 +30,8 @@ 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 , htmlB ody string ) * Message {
func NewMessageFrom ( to [ ] string , from , subject , b ody string ) * Message {
log . Trace ( "NewMessageFrom (htmlBody):\n%s" , htmlB ody )
log . Trace ( "NewMessageFrom (body):\n%s" , b ody )
msg := gomail . NewMessage ( )
msg := gomail . NewMessage ( )
msg . SetHeader ( "From" , from )
msg . SetHeader ( "From" , from )
@ -38,15 +39,15 @@ func NewMessageFrom(to []string, from, subject, htmlBody string) *Message {
msg . SetHeader ( "Subject" , subject )
msg . SetHeader ( "Subject" , subject )
msg . SetDateHeader ( "Date" , time . Now ( ) )
msg . SetDateHeader ( "Date" , time . Now ( ) )
body , err := html2text . FromString ( htmlBody )
plainBody , err := html2text . FromString ( body )
if err != nil {
if err != nil || setting . MailService . SendAsPlainText {
log . Error ( 4 , "html2text.FromString: %v" , err )
if strings . Contains ( body [ : 100 ] , "<html>" ) {
msg . SetBody ( "text/html" , htmlBody )
log . Warn ( "Mail contains HTML but configured to send as plain text." )
} else {
msg . SetBody ( "text/plain" , body )
if setting . MailService . EnableHTMLAlternative {
msg . AddAlternative ( "text/html" , htmlBody )
}
}
msg . SetBody ( "text/plain" , plainBody )
} else {
msg . SetBody ( "text/plain" , plainBody )
msg . AddAlternative ( "text/html" , body )
}
}
return & Message {
return & Message {