/
header.go
121 lines (95 loc) · 2.74 KB
/
header.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
110
111
112
113
114
115
116
117
118
119
120
121
package mail
import (
"bytes"
"errors"
"fmt"
"net/textproto"
"strings"
"time"
)
const TIME_FORMAT = time.RFC1123Z
type Header textproto.MIMEHeader
type HeaderParams map[string]interface{}
func (h Header) MIMEHeader() textproto.MIMEHeader {
return textproto.MIMEHeader(h)
}
func (h Header) SetString(key string, value string) {
h.set(key, getQEncodeString(value))
}
func (h Header) SetDate(key string, value time.Time) {
h.set(key, value.Format(TIME_FORMAT))
}
func (h Header) SetAddress(key string, value ...string) error {
buf := bytes.NewBuffer(nil)
for _, src := range value {
emails, err := parseAdresses(src)
if err != nil {
return errors.New(fmt.Sprintf("Field '%s': %s (addresses: %q)", key, err.Error(), value))
}
for _, e := range emails {
if buf.Len() > 0 {
buf.WriteString(",")
}
buf.WriteString(e.String())
}
}
// Result examples:
// - box@gmail.com
// - Alias <box@gmail.com>
// - box1@gmail.com; box2@gmail.com
// - Alias <box1@gmail.com>; box2@gmail.com
h.set(key, buf.String())
return nil
}
func (h Header) SetValue(key, value string, params HeaderParams) {
buf := bytes.NewBufferString(getQEncodeString(value))
if params != nil {
for k, v := range params {
buf.WriteString("; ")
var paramVal string
switch v.(type) {
case string:
paramVal = fmt.Sprintf(`"%s"`, getQEncodeString(v.(string)))
case *string:
paramVal = fmt.Sprintf(`"%s"`, getQEncodeString(*v.(*string)))
case time.Time:
paramVal = fmt.Sprintf(`"%s"`, v.(time.Time).Format(TIME_FORMAT))
case *time.Time:
paramVal = fmt.Sprintf(`"%s"`, v.(*time.Time).Format(TIME_FORMAT))
default:
paramVal = fmt.Sprintf(`%v`, v)
}
buf.WriteString(k)
buf.WriteString("=")
buf.WriteString(paramVal)
}
}
// Result examples:
// - image/jpeg; name="f7fb566a3f724874bf53cdb8e3b37d7a.jpg"
// - f7fb566a3f724874bf53cdb8e3b37d7a.jpg
// - attachment; filename="f7fb566a3f724874bf53cdb8e3b37d7a.jpg"; size=41681; creation-date="Tue, 27 Dec 2016 12:29:43 GMT"
// - base64
h.set(key, buf.String())
}
func (h Header) Bytes() []byte {
// Result example:
//
// Content-Type: image/jpeg; name="f7fb566a3f724874bf53cdb8e3b37d7a.jpg"
// Content-Description: f7fb566a3f724874bf53cdb8e3b37d7a.jpg
// Content-Disposition: attachment;
// filename="f7fb566a3f724874bf53cdb8e3b37d7a.jpg"; size=41681;
// creation-date="Tue, 27 Dec 2016 12:29:43 GMT";
// modification-date="Tue, 27 Dec 2016 12:29:43 GMT"
// Content-Transfer-Encoding: base64
buf := bytes.NewBuffer(nil)
for k, v := range h {
buf.WriteString(k)
buf.WriteString(": ")
buf.WriteString(strings.Join(v, "; "))
buf.WriteString("\r\n")
}
return buf.Bytes()
}
func (h Header) set(key string, value string) {
h[key] = []string{value}
}