-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
58 lines (44 loc) · 1.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package xmailerdaemon
import (
"net/mail"
"strings"
)
// IsDSN checks that message is valid Delivery Status Notification (DSN)
func IsDSN(message *mail.Message) bool {
if message == nil {
return false
}
hdr := message.Header.Get("X-Mailer-Daemon-Recipients")
hdr = strings.TrimSpace(hdr)
reasonHdr := message.Header.Get("X-Mailer-Daemon-Error")
reasonHdr = strings.TrimSpace(reasonHdr)
if hdr == "" || reasonHdr == "" {
return false
}
return true
}
// Parse parses Delivery Status Notification (DSN) from mail message and returs failed recipients list
func Parse(message *mail.Message) ([]Result, error) {
if message == nil {
return nil, ErrorNilMessage
}
hdr := message.Header.Get("X-Mailer-Daemon-Recipients")
hdr = strings.TrimSpace(hdr)
reasonHdr := message.Header.Get("X-Mailer-Daemon-Error")
reasonHdr = strings.TrimSpace(reasonHdr)
if hdr == "" || reasonHdr == "" {
return nil, ErrorDSNNotFound
}
recipients := strings.FieldsFunc(hdr, func(r rune) bool {
return r == ';' || r == ','
})
var ret []Result
// trim spaces from recipients
for i := range recipients {
ret = append(ret, Result{
Address: strings.TrimSpace(recipients[i]),
Reason: reasonHdr,
})
}
return ret, nil
}