@@ -9,6 +9,18 @@ import (
9
9
"time"
10
10
)
11
11
12
+ // LogHandler is called for each log message. If nil, log messages will
13
+ // be output using log.Printf instead.
14
+ var LogHandler func (message string , args ... interface {})
15
+
16
+ func logf (message string , args ... interface {}) {
17
+ if LogHandler != nil {
18
+ LogHandler (message , args ... )
19
+ } else {
20
+ log .Printf (message , args ... )
21
+ }
22
+ }
23
+
12
24
// MessageID represents the ID of an SMTP message including the hostname part
13
25
type MessageID string
14
26
@@ -90,7 +102,7 @@ func (m *SMTPMessage) Parse(hostname string) *Message {
90
102
}
91
103
92
104
if msg .Content .IsMIME () {
93
- log . Printf ("Parsing MIME body" )
105
+ logf ("Parsing MIME body" )
94
106
msg .MIME = msg .Content .ParseMIMEBody ()
95
107
}
96
108
@@ -120,16 +132,16 @@ func (content *Content) ParseMIMEBody() *MIMEBody {
120
132
var p []string
121
133
if len (boundary ) > 0 {
122
134
p = strings .Split (content .Body , "--" + boundary )
123
- log . Printf ("Got boundary: %s" , boundary )
135
+ logf ("Got boundary: %s" , boundary )
124
136
} else {
125
- log . Printf ("Boundary not found: %s" , hdr [0 ])
137
+ logf ("Boundary not found: %s" , hdr [0 ])
126
138
}
127
139
128
140
for _ , s := range p {
129
141
if len (s ) > 0 {
130
142
part := ContentFromString (strings .Trim (s , "\r \n " ))
131
143
if part .IsMIME () {
132
- log . Printf ("Parsing inner MIME body" )
144
+ logf ("Parsing inner MIME body" )
133
145
part .MIME = part .ParseMIMEBody ()
134
146
}
135
147
parts = append (parts , part )
@@ -171,7 +183,7 @@ func PathFromString(path string) *Path {
171
183
172
184
// ContentFromString parses SMTP content into separate headers and body
173
185
func ContentFromString (data string ) * Content {
174
- log . Printf ("Parsing Content from string: '%s'" , data )
186
+ logf ("Parsing Content from string: '%s'" , data )
175
187
x := strings .SplitN (data , "\r \n \r \n " , 2 )
176
188
h := make (map [string ][]string , 0 )
177
189
@@ -189,7 +201,7 @@ func ContentFromString(data string) *Content {
189
201
h [key ] = []string {value }
190
202
lastHdr = key
191
203
} else {
192
- log . Printf ("Found invalid header: '%s'" , hdr )
204
+ logf ("Found invalid header: '%s'" , hdr )
193
205
}
194
206
}
195
207
return & Content {
0 commit comments