NewMessageFrom creates new mail message object with custom From header.
(to []string, from, subject, htmlBody string)
| 25 | |
| 26 | // NewMessageFrom creates new mail message object with custom From header. |
| 27 | func NewMessageFrom(to []string, from, subject, htmlBody string) *Message { |
| 28 | log.Trace("NewMessageFrom (htmlBody):\n%s", htmlBody) |
| 29 | |
| 30 | msg := gomail.NewMessage() |
| 31 | msg.SetHeader("From", from) |
| 32 | msg.SetHeader("To", to...) |
| 33 | msg.SetHeader("Subject", conf.Email.SubjectPrefix+subject) |
| 34 | msg.SetDateHeader("Date", time.Now()) |
| 35 | |
| 36 | contentType := "text/html" |
| 37 | body := htmlBody |
| 38 | switchedToPlaintext := false |
| 39 | if conf.Email.UsePlainText || conf.Email.AddPlainTextAlt { |
| 40 | plainBody, err := html2text.FromString(htmlBody) |
| 41 | if err != nil { |
| 42 | log.Error("html2text.FromString: %v", err) |
| 43 | } else { |
| 44 | contentType = "text/plain" |
| 45 | body = plainBody |
| 46 | switchedToPlaintext = true |
| 47 | } |
| 48 | } |
| 49 | msg.SetBody(contentType, body) |
| 50 | if switchedToPlaintext && conf.Email.AddPlainTextAlt && !conf.Email.UsePlainText { |
| 51 | // The AddAlternative method name is confusing - adding html as an "alternative" will actually cause mail |
| 52 | // clients to show it as first priority, and the text "main body" is the 2nd priority fallback. |
| 53 | // See: https://godoc.org/gopkg.in/gomail.v2#Message.AddAlternative |
| 54 | msg.AddAlternative("text/html", htmlBody) |
| 55 | } |
| 56 | return &Message{ |
| 57 | Message: msg, |
| 58 | confirmChan: make(chan struct{}), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // NewMessage creates new mail message object with default From header. |
| 63 | func NewMessage(to []string, subject, body string) *Message { |
no test coverage detected