Default header and sorting

- Default headers are set in its own method now
- Generic headers are now sorted before being written
This commit is contained in:
Winni Neessen 2022-03-20 19:11:58 +01:00
parent 663954f370
commit 030eba8cfd
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 26 additions and 7 deletions

11
msg.go
View file

@ -605,6 +605,17 @@ func (m *Msg) checkUserAgent() {
}
}
// addDefaultHeader sets some default headers, if they haven't been set before
func (m *Msg) addDefaultHeader() {
if _, ok := m.genHeader[HeaderDate]; !ok {
m.SetDate()
}
if _, ok := m.genHeader[HeaderMessageID]; !ok {
m.SetMessageID()
}
m.SetHeader(HeaderMIMEVersion, string(m.mimever))
}
// fileFromFS returns a File pointer from a given file in the system's file system
func fileFromFS(n string) *File {
_, err := os.Stat(n)

View file

@ -10,6 +10,7 @@ import (
"mime/quotedprintable"
"net/textproto"
"path/filepath"
"sort"
"strings"
)
@ -43,13 +44,9 @@ func (mw *msgWriter) Write(p []byte) (int, error) {
// writeMsg formats the message and sends it to its io.Writer
func (mw *msgWriter) writeMsg(m *Msg) {
if _, ok := m.genHeader[HeaderDate]; !ok {
m.SetDate()
}
m.addDefaultHeader()
m.checkUserAgent()
for k, v := range m.genHeader {
mw.writeHeader(k, v...)
}
mw.writeGenHeader(m)
for _, t := range []AddrHeader{HeaderFrom, HeaderTo, HeaderCc} {
if al, ok := m.addrHeader[t]; ok {
var v []string
@ -59,7 +56,6 @@ func (mw *msgWriter) writeMsg(m *Msg) {
mw.writeHeader(Header(t), v...)
}
}
mw.writeHeader(HeaderMIMEVersion, string(m.mimever))
if m.hasMixed() {
mw.startMP("mixed", m.boundary)
@ -94,6 +90,18 @@ func (mw *msgWriter) writeMsg(m *Msg) {
}
}
// writeGenHeader writes out all generic headers to the msgWriter
func (mw *msgWriter) writeGenHeader(m *Msg) {
gk := make([]string, 0, len(m.genHeader))
for k := range m.genHeader {
gk = append(gk, string(k))
}
sort.Strings(gk)
for _, k := range gk {
mw.writeHeader(Header(k), m.genHeader[Header(k)]...)
}
}
// startMP writes a multipart beginning
func (mw *msgWriter) startMP(mt MIMEType, b string) {
mp := multipart.NewWriter(mw)