mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
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:
parent
d6dffc3071
commit
b852a5281a
3 changed files with 32 additions and 32 deletions
3
doc.go
3
doc.go
|
@ -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
11
msg.go
|
@ -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)
|
||||||
|
|
50
msgwriter.go
50
msgwriter.go
|
@ -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) {
|
mw.writeString("\r\n ")
|
||||||
nfl = len(v[i])
|
cl = MaxHeaderLength - 3
|
||||||
}
|
}
|
||||||
if cl-len(s) < 1 {
|
mw.writeString(v)
|
||||||
if p := strings.IndexByte(s, ' '); p != -1 {
|
if i < len(sfs) {
|
||||||
mw.writeString(s[:p])
|
mw.writeString(" ")
|
||||||
mw.writeString("\r\n ")
|
cl -= 1
|
||||||
mw.writeString(s[p:])
|
|
||||||
cl -= len(s[p:])
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if cl < 1 || cl-nfl < 1 {
|
cl -= len(v)
|
||||||
mw.writeString("\r\n")
|
|
||||||
cl = MaxHeaderLength - 4
|
|
||||||
if i != len(v) {
|
|
||||||
mw.writeString(" ")
|
|
||||||
cl -= 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mw.writeString(s)
|
|
||||||
cl -= len(s)
|
|
||||||
|
|
||||||
if i != len(v)-1 {
|
|
||||||
mw.writeString(", ")
|
|
||||||
cl -= 2
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
mw.writeString("\r\n")
|
mw.writeString("\r\n")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue