forked from forgejo/forgejo
allways set a message-id on mails (#17900)
* allways set a message-id on mails * Add unit tests for mailer & Message-ID Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
0ff18a808c
commit
b59875aa12
3 changed files with 66 additions and 5 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"bytes"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"io"
|
||||
"net"
|
||||
"net/smtp"
|
||||
|
@ -67,6 +68,10 @@ func (m *Message) ToMessage() *gomail.Message {
|
|||
msg.SetBody("text/plain", plainBody)
|
||||
msg.AddAlternative("text/html", m.Body)
|
||||
}
|
||||
|
||||
if len(msg.GetHeader("Message-ID")) == 0 {
|
||||
msg.SetHeader("Message-ID", m.generateAutoMessageID())
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
|
@ -75,6 +80,17 @@ func (m *Message) SetHeader(field string, value ...string) {
|
|||
m.Headers[field] = value
|
||||
}
|
||||
|
||||
func (m *Message) generateAutoMessageID() string {
|
||||
dateMs := m.Date.UnixNano() / 1e6
|
||||
h := fnv.New64()
|
||||
if len(m.To) > 0 {
|
||||
_, _ = h.Write([]byte(m.To[0]))
|
||||
}
|
||||
_, _ = h.Write([]byte(m.Subject))
|
||||
_, _ = h.Write([]byte(m.Body))
|
||||
return fmt.Sprintf("<autogen-%d-%016x@%s>", dateMs, h.Sum64(), setting.Domain)
|
||||
}
|
||||
|
||||
// NewMessageFrom creates new mail message object with custom From header.
|
||||
func NewMessageFrom(to []string, fromDisplayName, fromAddress, subject, body string) *Message {
|
||||
log.Trace("NewMessageFrom (body):\n%s", body)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue