-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
224 lines (199 loc) · 15.9 KB
/
message_test.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package MIMEMail
import (
//"bytes"
"bytes"
"html/template"
"net/mail"
//"os"
"testing"
"github.com/tike/MIMEMail/templated"
)
func MessageFactory() *Mail {
m := NewMail()
m.AddPerson("From", "你好 ma", "foobar@example.com")
m.AddAddress("To", mail.Address{Name: "Änja Süße", Address: "blabla@example.com"})
m.AddAddress("To", mail.Address{Name: "xiao mao", Address: "xiao_mao@example.com"})
m.Subject = "你好 Änja"
return m
}
func Test_Message_writeHeader(t *testing.T) {
exp := []byte{0x46, 0x72, 0x6f, 0x6d, 0x3a, 0x20, 0x3d, 0x3f, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x3f, 0x71, 0x3f, 0x3d, 0x45, 0x34, 0x3d,
0x42, 0x44, 0x3d, 0x41, 0x30, 0x3d, 0x45, 0x35, 0x3d, 0x41, 0x35, 0x3d, 0x42, 0x44, 0x5f, 0x6d, 0x61, 0x3f, 0x3d, 0x20, 0x3c, 0x66,
0x6f, 0x6f, 0x62, 0x61, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0xd, 0xa, 0x54, 0x6f,
0x3a, 0x20, 0x3d, 0x3f, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x3f, 0x71, 0x3f, 0x3d, 0x43, 0x33, 0x3d, 0x38, 0x34, 0x6e, 0x6a, 0x61, 0x5f,
0x53, 0x3d, 0x43, 0x33, 0x3d, 0x42, 0x43, 0x3d, 0x43, 0x33, 0x3d, 0x39, 0x46, 0x65, 0x3f, 0x3d, 0x20, 0x3c, 0x62, 0x6c, 0x61, 0x62,
0x6c, 0x61, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0xd, 0xa, 0x53, 0x75, 0x62, 0x6a, 0x65,
0x63, 0x74, 0x3a, 0x20, 0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd, 0x20, 0xc3, 0x84, 0x6e, 0x6a, 0x61, 0xd, 0xa, 0x4d, 0x49, 0x4d, 0x45,
0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0xd, 0xa, 0x43,
0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74,
0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x20, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x63, 0x35, 0x35, 0x31, 0x37,
0x61, 0x39, 0x35, 0x31, 0x36, 0x30, 0x33, 0x31, 0x32, 0x34, 0x36, 0x31, 0x32, 0x31, 0x30, 0x66, 0x61, 0x38, 0x66, 0x37, 0x38, 0x37,
0x36, 0x64, 0x36, 0x65, 0x31, 0x34, 0x66, 0x33, 0x30, 0x63, 0x37, 0x35, 0x30, 0x62, 0x65, 0x38, 0x34, 0x34, 0x64, 0x32, 0x35, 0x66,
0x31, 0x39, 0x64, 0x36, 0x63, 0x31, 0x32, 0x36, 0x32, 0x30, 0x37, 0xd, 0xa, 0xd, 0xa, 0xd, 0xa, 0x2d, 0x2d, 0x63, 0x35, 0x35, 0x31,
0x37, 0x61, 0x39, 0x35, 0x31, 0x36, 0x30, 0x33, 0x31, 0x32, 0x34, 0x36, 0x31, 0x32, 0x31, 0x30, 0x66, 0x61, 0x38, 0x66, 0x37, 0x38,
0x37, 0x36, 0x64, 0x36, 0x65, 0x31, 0x34, 0x66, 0x33, 0x30, 0x63, 0x37, 0x35, 0x30, 0x62, 0x65, 0x38, 0x34, 0x34, 0x64, 0x32, 0x35,
0x66, 0x31, 0x39, 0x64, 0x36, 0x63, 0x31, 0x32, 0x36, 0x32, 0x30, 0x37, 0x2d, 0x2d, 0xd, 0xa}
expBound := []byte{0x63, 0x35, 0x35, 0x31, 0x37, 0x61, 0x39, 0x35, 0x31, 0x36, 0x30, 0x33, 0x31, 0x32, 0x34, 0x36, 0x31, 0x32, 0x31,
0x30, 0x66, 0x61, 0x38, 0x66, 0x37, 0x38, 0x37, 0x36, 0x64, 0x36, 0x65, 0x31, 0x34, 0x66, 0x33, 0x30, 0x63, 0x37, 0x35, 0x30, 0x62,
0x65, 0x38, 0x34, 0x34, 0x64, 0x32, 0x35, 0x66, 0x31, 0x39, 0x64, 0x36, 0x63, 0x31, 0x32, 0x36, 0x32, 0x30, 0x37}
m := MessageFactory()
b, err := m.Bytes()
if err != nil {
t.Fatal(err)
}
expected := bytes.Replace(exp, expBound, []byte(m.boundary), -1)
if bytes.Compare(expected, b) != 0 {
t.Fatalf("expected \n%s\nbut got:\n%s\n", string(exp), string(b))
}
}
func Test_Message_Body(t *testing.T) {
exp := []byte{0x46, 0x72, 0x6f, 0x6d, 0x3a, 0x20, 0x3d, 0x3f, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x3f, 0x71, 0x3f, 0x3d, 0x45, 0x34, 0x3d, 0x42,
0x44, 0x3d, 0x41, 0x30, 0x3d, 0x45, 0x35, 0x3d, 0x41, 0x35, 0x3d, 0x42, 0x44, 0x5f, 0x6d, 0x61, 0x3f, 0x3d, 0x20, 0x3c, 0x66, 0x6f, 0x6f,
0x62, 0x61, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0xd, 0xa, 0x54, 0x6f, 0x3a, 0x20, 0x3d,
0x3f, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x3f, 0x71, 0x3f, 0x3d, 0x43, 0x33, 0x3d, 0x38, 0x34, 0x6e, 0x6a, 0x61, 0x5f, 0x53, 0x3d, 0x43, 0x33,
0x3d, 0x42, 0x43, 0x3d, 0x43, 0x33, 0x3d, 0x39, 0x46, 0x65, 0x3f, 0x3d, 0x20, 0x3c, 0x62, 0x6c, 0x61, 0x62, 0x6c, 0x61, 0x40, 0x65, 0x78,
0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0xd, 0xa, 0x53, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x3a, 0x20, 0xe4, 0xbd, 0xa0,
0xe5, 0xa5, 0xbd, 0x20, 0xc3, 0x84, 0x6e, 0x6a, 0x61, 0xd, 0xa, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a,
0x20, 0x31, 0x2e, 0x30, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70,
0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x20, 0x62, 0x6f, 0x75, 0x6e,
0x64, 0x61, 0x72, 0x79, 0x3d, 0x62, 0x30, 0x39, 0x37, 0x61, 0x34, 0x32, 0x65, 0x33, 0x30, 0x38, 0x66, 0x65, 0x30, 0x37, 0x30, 0x31, 0x33, 0x32,
0x36, 0x37, 0x33, 0x34, 0x34, 0x33, 0x37, 0x65, 0x64, 0x33, 0x61, 0x37, 0x39, 0x35, 0x39, 0x66, 0x35, 0x39, 0x35, 0x63, 0x33, 0x34, 0x39, 0x38,
0x62, 0x61, 0x31, 0x33, 0x35, 0x31, 0x36, 0x31, 0x39, 0x35, 0x31, 0x39, 0x65, 0x32, 0x39, 0x61, 0x36, 0xd, 0xa, 0xd, 0xa, 0x2d, 0x2d, 0x62, 0x30,
0x39, 0x37, 0x61, 0x34, 0x32, 0x65, 0x33, 0x30, 0x38, 0x66, 0x65, 0x30, 0x37, 0x30, 0x31, 0x33, 0x32, 0x36, 0x37, 0x33, 0x34, 0x34, 0x33, 0x37,
0x65, 0x64, 0x33, 0x61, 0x37, 0x39, 0x35, 0x39, 0x66, 0x35, 0x39, 0x35, 0x63, 0x33, 0x34, 0x39, 0x38, 0x62, 0x61, 0x31, 0x33, 0x35, 0x31, 0x36,
0x31, 0x39, 0x35, 0x31, 0x39, 0x65, 0x32, 0x39, 0x61, 0x36, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65,
0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3b, 0x20, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x75, 0x74, 0x66,
0x2d, 0x38, 0xd, 0xa, 0xd, 0xa, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0xa, 0x9, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x68, 0x31,
0x20, 0x69, 0x64, 0x3d, 0x22, 0x62, 0x6c, 0x61, 0x22, 0x3e, 0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x3c,
0x2f, 0x68, 0x31, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x70, 0x3e, 0x49, 0x20, 0x68, 0x65, 0x61, 0x72, 0x64, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6c, 0x69, 0x6b,
0x65, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x4d, 0x61, 0x69, 0x6c, 0x73, 0x2c, 0x20, 0x73, 0x6f, 0x20, 0x49, 0x20, 0x70, 0x75, 0x74, 0x3c, 0x2f,
0x70, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x75, 0x6c, 0x3e, 0xa, 0x9, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x6c, 0x69, 0x3e, 0x4d, 0x49,
0x4d, 0x45, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x4d, 0x61, 0x69, 0x6c, 0x62, 0x6f, 0x64,
0x79, 0x3c, 0x2f, 0x6c, 0x69, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x2f, 0x75, 0x6c, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x70, 0x3e, 0x53, 0x6f, 0x20, 0x74, 0x68,
0x61, 0x74, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x63, 0x61, 0x6e, 0x3c, 0x2f, 0x70, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x75, 0x6c, 0x3e, 0xa, 0x9, 0x9, 0x9,
0x3c, 0x6c, 0x69, 0x3e, 0x73, 0x65, 0x6e, 0x64, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x70, 0x61, 0x72, 0x74, 0x73, 0x20, 0x77, 0x68, 0x69, 0x6c, 0x65,
0x20, 0x79, 0x6f, 0x75, 0x20, 0x4d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x3c, 0x2f, 0x6c, 0x69, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x2f, 0x75,
0x6c, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x70, 0x3e, 0x79, 0x6f, 0x75, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x62, 0x65, 0x65, 0x6e, 0x20, 0x70, 0x69, 0x6d,
0x70, 0x65, 0x64, 0x21, 0x3c, 0x2f, 0x70, 0x3e, 0xa, 0x9, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0xa, 0x9, 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c,
0x3e, 0xa, 0x9, 0xd, 0xa, 0x2d, 0x2d, 0x62, 0x30, 0x39, 0x37, 0x61, 0x34, 0x32, 0x65, 0x33, 0x30, 0x38, 0x66, 0x65, 0x30, 0x37, 0x30, 0x31, 0x33,
0x32, 0x36, 0x37, 0x33, 0x34, 0x34, 0x33, 0x37, 0x65, 0x64, 0x33, 0x61, 0x37, 0x39, 0x35, 0x39, 0x66, 0x35, 0x39, 0x35, 0x63, 0x33, 0x34, 0x39,
0x38, 0x62, 0x61, 0x31, 0x33, 0x35, 0x31, 0x36, 0x31, 0x39, 0x35, 0x31, 0x39, 0x65, 0x32, 0x39, 0x61, 0x36, 0x2d, 0x2d, 0xd, 0xa}
expBound := []byte{0x62, 0x30, 0x39, 0x37, 0x61, 0x34, 0x32, 0x65, 0x33, 0x30, 0x38, 0x66, 0x65, 0x30, 0x37, 0x30, 0x31, 0x33, 0x32, 0x36, 0x37, 0x33,
0x34, 0x34, 0x33, 0x37, 0x65, 0x64, 0x33, 0x61, 0x37, 0x39, 0x35, 0x39, 0x66, 0x35, 0x39, 0x35, 0x63, 0x33, 0x34, 0x39, 0x38, 0x62, 0x61, 0x31, 0x33,
0x35, 0x31, 0x36, 0x31, 0x39, 0x35, 0x31, 0x39, 0x65, 0x32, 0x39, 0x61, 0x36}
m := MessageFactory()
tmpl, err := template.New("body").Parse(htmlBody)
if err != nil {
t.Fatal(err)
}
if err = tmpl.ExecuteTemplate(m.HTMLBody(), "body", nil); err != nil {
t.Fatal(err)
}
b, err := m.Bytes()
if err != nil {
t.Fatal(err)
}
expected := bytes.Replace(exp, expBound, []byte(m.boundary), -1)
if bytes.Compare(expected, b) != 0 {
t.Fatalf("expected \n%s\nbut got:\n%s\n", string(exp), string(b))
}
}
func Test_Message_Attach(t *testing.T) {
exp := []byte{0x46, 0x72, 0x6f, 0x6d, 0x3a, 0x20, 0x3d, 0x3f, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x3f, 0x71, 0x3f, 0x3d, 0x45, 0x34, 0x3d, 0x42,
0x44, 0x3d, 0x41, 0x30, 0x3d, 0x45, 0x35, 0x3d, 0x41, 0x35, 0x3d, 0x42, 0x44, 0x5f, 0x6d, 0x61, 0x3f, 0x3d, 0x20, 0x3c, 0x66, 0x6f, 0x6f,
0x62, 0x61, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0xd, 0xa, 0x54, 0x6f, 0x3a, 0x20, 0x3d, 0x3f,
0x75, 0x74, 0x66, 0x2d, 0x38, 0x3f, 0x71, 0x3f, 0x3d, 0x43, 0x33, 0x3d, 0x38, 0x34, 0x6e, 0x6a, 0x61, 0x5f, 0x53, 0x3d, 0x43, 0x33, 0x3d, 0x42,
0x43, 0x3d, 0x43, 0x33, 0x3d, 0x39, 0x46, 0x65, 0x3f, 0x3d, 0x20, 0x3c, 0x62, 0x6c, 0x61, 0x62, 0x6c, 0x61, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0xd, 0xa, 0x53, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x3a, 0x20, 0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd, 0x20,
0xc3, 0x84, 0x6e, 0x6a, 0x61, 0xd, 0xa, 0x4d, 0x49, 0x4d, 0x45, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x6d, 0x75, 0x6c,
0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x20, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x3d, 0x36, 0x34,
0x30, 0x34, 0x32, 0x62, 0x35, 0x63, 0x66, 0x65, 0x36, 0x33, 0x35, 0x62, 0x33, 0x35, 0x63, 0x65, 0x61, 0x63, 0x63, 0x32, 0x36, 0x64, 0x34, 0x36, 0x37,
0x31, 0x64, 0x66, 0x30, 0x36, 0x38, 0x32, 0x37, 0x66, 0x61, 0x38, 0x64, 0x37, 0x35, 0x62, 0x31, 0x61, 0x39, 0x31, 0x64, 0x36, 0x63, 0x39, 0x61, 0x63,
0x61, 0x34, 0x38, 0x39, 0x64, 0x64, 0x34, 0x63, 0xd, 0xa, 0xd, 0xa, 0x2d, 0x2d, 0x36, 0x34, 0x30, 0x34, 0x32, 0x62, 0x35, 0x63, 0x66, 0x65, 0x36, 0x33,
0x35, 0x62, 0x33, 0x35, 0x63, 0x65, 0x61, 0x63, 0x63, 0x32, 0x36, 0x64, 0x34, 0x36, 0x37, 0x31, 0x64, 0x66, 0x30, 0x36, 0x38, 0x32, 0x37, 0x66, 0x61,
0x38, 0x64, 0x37, 0x35, 0x62, 0x31, 0x61, 0x39, 0x31, 0x64, 0x36, 0x63, 0x39, 0x61, 0x63, 0x61, 0x34, 0x38, 0x39, 0x64, 0x64, 0x34, 0x63, 0xd, 0xa,
0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x44, 0x69, 0x73, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x61, 0x74, 0x74, 0x61,
0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x3b, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x5f, 0x61, 0x74,
0x74, 0x61, 0x63, 0x68, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x74, 0x78, 0x74, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x72, 0x61, 0x6e,
0x73, 0x66, 0x65, 0x72, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x20, 0x62, 0x61, 0x73, 0x65, 0x36, 0x34, 0xd, 0xa, 0x43, 0x6f, 0x6e,
0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6f, 0x63, 0x74,
0x65, 0x74, 0x2d, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0xd, 0xa, 0xd, 0xa, 0x53, 0x53, 0x64, 0x74, 0x49, 0x47, 0x45, 0x67, 0x63, 0x32, 0x68, 0x76, 0x63, 0x6e,
0x51, 0x67, 0x59, 0x58, 0x52, 0x30, 0x59, 0x57, 0x4e, 0x6f, 0x62, 0x57, 0x56, 0x75, 0x64, 0x43, 0x45, 0x4b, 0xd, 0xa, 0x2d, 0x2d, 0x36, 0x34, 0x30, 0x34, 0x32,
0x62, 0x35, 0x63, 0x66, 0x65, 0x36, 0x33, 0x35, 0x62, 0x33, 0x35, 0x63, 0x65, 0x61, 0x63, 0x63, 0x32, 0x36, 0x64, 0x34, 0x36, 0x37, 0x31, 0x64, 0x66, 0x30,
0x36, 0x38, 0x32, 0x37, 0x66, 0x61, 0x38, 0x64, 0x37, 0x35, 0x62, 0x31, 0x61, 0x39, 0x31, 0x64, 0x36, 0x63, 0x39, 0x61, 0x63, 0x61, 0x34, 0x38, 0x39, 0x64,
0x64, 0x34, 0x63, 0xd, 0xa, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x68, 0x74,
0x6d, 0x6c, 0x3b, 0x20, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x75, 0x74, 0x66, 0x2d, 0x38, 0xd, 0xa, 0xd, 0xa, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
0x3e, 0xa, 0x9, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x68, 0x31, 0x20, 0x69, 0x64, 0x3d, 0x22, 0x62, 0x6c, 0x61, 0x22, 0x3e, 0xe4, 0xbd,
0xa0, 0xe5, 0xa5, 0xbd, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x70, 0x3e, 0x49, 0x20, 0x68, 0x65, 0x61,
0x72, 0x64, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x20, 0x4d, 0x61, 0x69, 0x6c, 0x73, 0x2c, 0x20, 0x73, 0x6f,
0x20, 0x49, 0x20, 0x70, 0x75, 0x74, 0x3c, 0x2f, 0x70, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x75, 0x6c, 0x3e, 0xa, 0x9, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x3c, 0x6c, 0x69, 0x3e, 0x4d, 0x49, 0x4d, 0x45, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x4d, 0x61, 0x69,
0x6c, 0x62, 0x6f, 0x64, 0x79, 0x3c, 0x2f, 0x6c, 0x69, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x2f, 0x75, 0x6c, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x70, 0x3e, 0x53, 0x6f, 0x20,
0x74, 0x68, 0x61, 0x74, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x63, 0x61, 0x6e, 0x3c, 0x2f, 0x70, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x75, 0x6c, 0x3e, 0xa, 0x9, 0x9, 0x9,
0x3c, 0x6c, 0x69, 0x3e, 0x73, 0x65, 0x6e, 0x64, 0x20, 0x4d, 0x49, 0x4d, 0x45, 0x70, 0x61, 0x72, 0x74, 0x73, 0x20, 0x77, 0x68, 0x69, 0x6c, 0x65, 0x20, 0x79,
0x6f, 0x75, 0x20, 0x4d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x3c, 0x2f, 0x6c, 0x69, 0x3e, 0xa, 0x9, 0x9, 0x3c, 0x2f, 0x75, 0x6c, 0x3e, 0xa, 0x9,
0x9, 0x3c, 0x70, 0x3e, 0x79, 0x6f, 0x75, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x62, 0x65, 0x65, 0x6e, 0x20, 0x70, 0x69, 0x6d, 0x70, 0x65, 0x64, 0x21, 0x3c,
0x2f, 0x70, 0x3e, 0xa, 0x9, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0xa, 0x9, 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0xa, 0x9, 0xd, 0xa, 0x2d, 0x2d,
0x36, 0x34, 0x30, 0x34, 0x32, 0x62, 0x35, 0x63, 0x66, 0x65, 0x36, 0x33, 0x35, 0x62, 0x33, 0x35, 0x63, 0x65, 0x61, 0x63, 0x63, 0x32, 0x36, 0x64, 0x34, 0x36,
0x37, 0x31, 0x64, 0x66, 0x30, 0x36, 0x38, 0x32, 0x37, 0x66, 0x61, 0x38, 0x64, 0x37, 0x35, 0x62, 0x31, 0x61, 0x39, 0x31, 0x64, 0x36, 0x63, 0x39, 0x61, 0x63,
0x61, 0x34, 0x38, 0x39, 0x64, 0x64, 0x34, 0x63, 0x2d, 0x2d, 0xd, 0xa}
expBound := []byte{0x36, 0x34, 0x30, 0x34, 0x32, 0x62, 0x35, 0x63, 0x66, 0x65, 0x36, 0x33, 0x35, 0x62, 0x33, 0x35, 0x63, 0x65, 0x61, 0x63, 0x63, 0x32, 0x36,
0x64, 0x34, 0x36, 0x37, 0x31, 0x64, 0x66, 0x30, 0x36, 0x38, 0x32, 0x37, 0x66, 0x61, 0x38, 0x64, 0x37, 0x35, 0x62, 0x31, 0x61, 0x39, 0x31, 0x64, 0x36, 0x63,
0x39, 0x61, 0x63, 0x61, 0x34, 0x38, 0x39, 0x64, 0x64, 0x34, 0x63}
m := MessageFactory()
if err := m.AddReader("short_attachment.txt", bytes.NewBuffer([]byte(shortAttachment))); err != nil {
t.Fatalf("opening attachment: %s", err)
}
tmpl, err := template.New("body").Parse(htmlBody)
if err != nil {
t.Fatal(err)
}
if err = tmpl.ExecuteTemplate(m.HTMLBody(), "body", []string{"foo", "bar", "baz"}); err != nil {
t.Fatal(err)
}
b, err := m.Bytes()
if err != nil {
t.Fatal(err)
}
expected := bytes.Replace(exp, expBound, []byte(m.boundary), -1)
if bytes.Compare(expected, b) != 0 {
t.Fatalf("expected \n%s\nbut got:\n%s\n", string(exp), string(b))
}
}
const (
htmlBody = `{{ define "body" }}<html>
<body>
<h1 id="bla">你好 world!</h1>
<p>I heard you like MIME Mails, so I put</p>
<ul>
<li>MIMEHeader in your Mailbody</li>
</ul>
<p>So that you can</p>
<ul>
<li>send MIMEparts while you Multipart</li>
</ul>
<p>you have been pimped!</p>
</body>
</html>
{{ end }}`
shortAttachment = `I'm a short attachment!
`
)
func TestMailTemplated(t *testing.T) {
cnf := templated.Config{Dir: "templated/example", Lang: "en_US"}
ctx := map[string]interface{}{
"Name": "Mr. Receiver",
"Company": "MIMEMail",
}
m, err := NewTemplated(&cnf, "foo", ctx)
if err != nil {
t.Fatal(err)
}
m.To("Mr. Receiver", "receiver@example.com")
m.From("Mr. Sender", "sender@example.com")
out, err := m.Bytes()
if err != nil {
t.Fatal(err)
}
t.Log(string(out))
}