v0.1.6: Fix in writeHeader() and User-Agent

- The writeHeader() method wasn't producing good output for long headers. This has been fixed
- Added a VERSION string to the library
- If both no User-Agent and no X-Mailer header are set, the lib adds a default UA-header
This commit is contained in:
Winni Neessen 2022-03-20 17:38:46 +01:00
parent d6dffc3071
commit b852a5281a
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
3 changed files with 32 additions and 32 deletions

3
doc.go
View file

@ -1,2 +1,5 @@
// Package mail provides a simple and easy way to send mails with Go // Package mail provides a simple and easy way to send mails with Go
package mail package mail
// VERSION is used in the default user agent string
const VERSION = "0.1.6"

11
msg.go
View file

@ -594,6 +594,17 @@ func (m *Msg) setEncoder() {
m.encoder = getEncoder(m.encoding) m.encoder = getEncoder(m.encoding)
} }
// checkUserAgent checks if a useragent/x-mailer is set and if not will set a default
// version string
func (m *Msg) checkUserAgent() {
_, uaok := m.genHeader[HeaderUserAgent]
_, xmok := m.genHeader[HeaderXMailer]
if !uaok && !xmok {
m.SetUserAgent(fmt.Sprintf("go-mail v%s // https://github.com/wneessen/go-mail",
VERSION))
}
}
// fileFromFS returns a File pointer from a given file in the system's file system // fileFromFS returns a File pointer from a given file in the system's file system
func fileFromFS(n string) *File { func fileFromFS(n string) *File {
_, err := os.Stat(n) _, err := os.Stat(n)

View file

@ -46,6 +46,7 @@ func (mw *msgWriter) writeMsg(m *Msg) {
if _, ok := m.genHeader[HeaderDate]; !ok { if _, ok := m.genHeader[HeaderDate]; !ok {
m.SetDate() m.SetDate()
} }
m.checkUserAgent()
for k, v := range m.genHeader { for k, v := range m.genHeader {
mw.writeHeader(k, v...) mw.writeHeader(k, v...)
} }
@ -197,46 +198,31 @@ func (mw *msgWriter) writeString(s string) {
} }
// writeHeader writes a header into the msgWriter's io.Writer // writeHeader writes a header into the msgWriter's io.Writer
func (mw *msgWriter) writeHeader(k Header, v ...string) { func (mw *msgWriter) writeHeader(k Header, vl ...string) {
// Chars left: MaxHeaderLength - "<Headername>: " - "CRLF"
cl := MaxHeaderLength - 2
mw.writeString(string(k)) mw.writeString(string(k))
if len(v) == 0 { cl -= len(k)
if len(vl) == 0 {
mw.writeString(":\r\n") mw.writeString(":\r\n")
return return
} }
mw.writeString(": ") mw.writeString(": ")
cl -= 2
// Chars left: MaxHeaderLength - "<Headername>: " - "CRLF" fs := strings.Join(vl, ", ")
cl := MaxHeaderLength - len(k) - 4 sfs := strings.Split(fs, " ")
for i, s := range v { for i, v := range sfs {
nfl := 0 if cl-len(v) <= 1 {
if i < len(v) {
nfl = len(v[i])
}
if cl-len(s) < 1 {
if p := strings.IndexByte(s, ' '); p != -1 {
mw.writeString(s[:p])
mw.writeString("\r\n ") mw.writeString("\r\n ")
mw.writeString(s[p:]) cl = MaxHeaderLength - 3
cl -= len(s[p:])
continue
} }
} mw.writeString(v)
if cl < 1 || cl-nfl < 1 { if i < len(sfs) {
mw.writeString("\r\n")
cl = MaxHeaderLength - 4
if i != len(v) {
mw.writeString(" ") mw.writeString(" ")
cl -= 1 cl -= 1
} }
} cl -= len(v)
mw.writeString(s)
cl -= len(s)
if i != len(v)-1 {
mw.writeString(", ")
cl -= 2
}
} }
mw.writeString("\r\n") mw.writeString("\r\n")
} }