-
Notifications
You must be signed in to change notification settings - Fork 0
/
getpart.go
108 lines (101 loc) · 2.71 KB
/
getpart.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package bounce_collector
import (
"bufio"
"bytes"
"fmt"
"io"
"mime"
"mime/multipart"
"net/mail"
"net/textproto"
"regexp"
"strings"
)
//func getMailFromRFC822Part(msg *mail.Message) (*mail.Message, error) {
// mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
// if err != nil {
// return nil, err
// }
// if strings.HasPrefix(mediaType, "multipart/") {
// mr := multipart.NewReader(msg.Body, params["boundary"])
// for {
// p, err := mr.NextPart()
// if err == io.EOF {
// return nil, fmt.Errorf("this message doesn't have rfc822 part")
// }
// if err != nil {
// return nil, err
// }
//
// if mt, _, e := mime.ParseMediaType(p.Header.Get("Content-Type")); strings.Contains(mt, "rfc822") && e == nil {
// m, err := mail.ReadMessage(p)
// if err != nil {
// return nil, err
// }
// return m, nil
// }
// }
// }
// return nil, fmt.Errorf("this not multipart message")
//}
func getHeadersFromRFC822Part(msg *mail.Message) (*textproto.MIMEHeader, error) {
mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
if err != nil {
return nil, err
}
if strings.HasPrefix(mediaType, "multipart/") {
mr := multipart.NewReader(msg.Body, params["boundary"])
for {
p, err := mr.NextPart()
if err == io.EOF {
return nil, fmt.Errorf("this message doesn't have rfc822 part")
}
if err != nil {
return nil, err
}
if mt, _, e := mime.ParseMediaType(p.Header.Get("Content-Type")); strings.Contains(mt, "rfc822") && e == nil {
tp := textproto.NewReader(bufio.NewReader(p))
hdr, err := tp.ReadMIMEHeader()
if err != nil && err != io.EOF {
return nil, err
}
return &hdr, nil
}
}
}
return nil, fmt.Errorf("this not multipart message")
}
var reMailFromTextPart = regexp.MustCompile(`(?m:^[\r,\n]+)(?ms:^[a-zA-Z0-9\-]{2,25}:.*)`)
//func getMailFromTextPart(msg *mail.Message) (*mail.Message, error) {
// b, err := io.ReadAll(msg.Body)
// if err != nil {
// return nil, err
// }
// reMail := reMailFromTextPart.FindIndex(b)
// if len(reMail) != 0 {
// mr := bytes.NewReader(b[reMail[0]+2:])
// m, err := mail.ReadMessage(mr)
// if err != nil {
// return nil, err
// }
// return m, nil
// }
// return nil, fmt.Errorf("mail not found in text")
//}
func getHeadersFromTextPart(msg *mail.Message) (*textproto.MIMEHeader, error) {
b, err := io.ReadAll(msg.Body)
if err != nil {
return nil, err
}
reMail := reMailFromTextPart.FindIndex(b)
if len(reMail) != 0 {
mr := bytes.NewReader(b[reMail[0]+2:])
tp := textproto.NewReader(bufio.NewReader(mr))
hdr, err := tp.ReadMIMEHeader()
if err != nil && err != io.EOF {
return nil, err
}
return &hdr, nil
}
return nil, fmt.Errorf("mail not found in text")
}