/
mailutil.go
109 lines (100 loc) · 2.73 KB
/
mailutil.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
109
package mailutil
import "github.com/sloonz/go-qprintable"
//import "code.google.com/p/go-charset/charset"
import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/mail"
"strings"
"unicode/utf8"
)
func HTMLBody(msg *mail.Message) (body string, err error) {
return toString(BodyOfType(msg, "text/html"))
}
func TextBody(msg *mail.Message) (body string, err error) {
return toString(BodyOfType(msg, "text/plain"))
}
func toString(r io.Reader, err error) (string, error) {
if r == nil || err != nil {
return "", err
}
bodyBytes, err := ioutil.ReadAll(r)
if err == nil {
// r, _ := charset.NewReader("UTF-8")
if utf8.ValidString(string(bodyBytes)) {
return string(bodyBytes), nil
}
}
return "", err
}
func BodyOfType(msg *mail.Message, mimetype string) (body io.Reader, err error) {
typeheader := msg.Header.Get("Content-Type")
if strings.HasPrefix(typeheader, mimetype) {
body = msg.Body
} else if strings.HasPrefix(typeheader, "multipart/") {
body, err = MultipartBodyOfType(msg, mimetype)
}
return
}
func MultipartBodyOfType(msg *mail.Message, mimetype string) (body io.Reader, err error) {
var mediaType string
var params map[string]string
if mediaType, params, err = mime.ParseMediaType(msg.Header.Get("Content-Type")); err != nil {
return
}
if !strings.HasPrefix(mediaType, "multipart/") {
err = errors.New("Content-Type is not multipart")
return
}
body, err = parseMultipart(msg.Body, params["boundary"], mimetype)
return
}
// from https://github.com/bradfitz/websomtep/blob/master/websomtep.go
func parseMultipart(r io.Reader, boundary string, mimetype string) (body io.Reader, err error) {
mr := multipart.NewReader(r, boundary)
for {
var part *multipart.Part
part, err = mr.NextPart()
if err == io.EOF {
break
}
if err != nil {
return
}
partType, partParams, _ := mime.ParseMediaType(part.Header.Get("Content-Type"))
if strings.HasPrefix(partType, "multipart/") {
body, err = parseMultipart(part, partParams["boundary"], mimetype)
if err != nil {
// log.Printf("in boundary %q, returning error for multipart child %q: %v", boundary, partParams["boundary"], err)
return
}
continue
}
if !strings.HasPrefix(partType, "text/") {
continue
}
if partType == mimetype {
body, err = decodePartBody(part)
return
}
}
return
}
func decodePartBody(part *multipart.Part) (body io.Reader, err error) {
enctype := part.Header.Get("Content-Transfer-Encoding")
if enctype == "" {
body = part
return
}
if strings.HasPrefix(enctype, "quoted-printable") {
body = qprintable.NewDecoder(qprintable.UnixTextEncoding, part)
} else if enctype == "7bit" {
} else {
err = errors.New(fmt.Sprintf("unhandled Content-Transfer-Encoding: %s", enctype))
}
return
}