forked from jordan-wright/email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_test.go
953 lines (850 loc) · 29.8 KB
/
email_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
package email
import (
"fmt"
"strings"
"testing"
"bufio"
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"net/smtp"
"net/textproto"
)
func prepareEmail() *Email {
e := NewEmail()
e.From = "Jordan Wright <[email protected]>"
e.To = []string{"[email protected]"}
e.Bcc = []string{"[email protected]"}
e.Cc = []string{"[email protected]"}
e.Subject = "Awesome Subject"
return e
}
func basicTests(t *testing.T, e *Email) *mail.Message {
raw, err := e.Bytes()
if err != nil {
t.Fatal("Failed to render message: ", e)
}
msg, err := mail.ReadMessage(bytes.NewBuffer(raw))
if err != nil {
t.Fatal("Could not parse rendered message: ", err)
}
expectedHeaders := map[string]string{
"To": "<[email protected]>",
"From": "\"Jordan Wright\" <[email protected]>",
"Cc": "<[email protected]>",
"Subject": "Awesome Subject",
}
for header, expected := range expectedHeaders {
if val := msg.Header.Get(header); val != expected {
t.Errorf("Wrong value for message header %s: %v != %v", header, expected, val)
}
}
return msg
}
func TestEmailText(t *testing.T) {
e := prepareEmail()
e.Text = []byte("Text Body is, of course, supported!\n")
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, _, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "text/plain" {
t.Fatalf("Content-type expected \"text/plain\", not %v", mt)
}
}
func TestEmailWithHTMLAttachments(t *testing.T) {
e := prepareEmail()
// Set plain text to exercise "mime/alternative"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<html><body>This is a text.</body></html>")
// Set HTML attachment to exercise "mime/related".
attachment, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "image/png; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
attachment.HTMLRelated = true
b, err := e.Bytes()
if err != nil {
t.Fatal("Could not serialize e-mail:", err)
}
// Print the bytes for ocular validation and make sure no errors.
//fmt.Println(string(b))
// TODO: Verify the attachments.
s := &trimReader{rd: bytes.NewBuffer(b)}
tp := textproto.NewReader(bufio.NewReader(s))
// Parse the main headers
hdrs, err := tp.ReadMIMEHeader()
if err != nil {
t.Fatal("Could not parse the headers:", err)
}
// Recursively parse the MIME parts
ps, err := parseMIMEParts(hdrs, tp.R)
if err != nil {
t.Fatal("Could not parse the MIME parts recursively:", err)
}
plainTextFound := false
htmlFound := false
imageFound := false
if expected, actual := 3, len(ps); actual != expected {
t.Error("Unexpected number of parts. Expected:", expected, "Was:", actual)
}
for _, part := range ps {
// part has "header" and "body []byte"
cd := part.header.Get("Content-Disposition")
ct := part.header.Get("Content-Type")
if strings.Contains(ct, "image/png") && strings.HasPrefix(cd, "inline") {
imageFound = true
}
if strings.Contains(ct, "text/html") {
htmlFound = true
}
if strings.Contains(ct, "text/plain") {
plainTextFound = true
}
}
if !plainTextFound {
t.Error("Did not find plain text part.")
}
if !htmlFound {
t.Error("Did not find HTML part.")
}
if !imageFound {
t.Error("Did not find image part.")
}
}
func TestEmailWithHTMLAttachmentsHTMLOnly(t *testing.T) {
e := prepareEmail()
e.HTML = []byte("<html><body>This is a text.</body></html>")
// Set HTML attachment to exercise "mime/related".
attachment, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "image/png; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
attachment.HTMLRelated = true
b, err := e.Bytes()
if err != nil {
t.Fatal("Could not serialize e-mail:", err)
}
// Print the bytes for ocular validation and make sure no errors.
//fmt.Println(string(b))
// TODO: Verify the attachments.
s := &trimReader{rd: bytes.NewBuffer(b)}
tp := textproto.NewReader(bufio.NewReader(s))
// Parse the main headers
hdrs, err := tp.ReadMIMEHeader()
if err != nil {
t.Fatal("Could not parse the headers:", err)
}
if !strings.HasPrefix(hdrs.Get("Content-Type"), "multipart/related") {
t.Error("Envelope Content-Type is not multipart/related: ", hdrs["Content-Type"])
}
// Recursively parse the MIME parts
ps, err := parseMIMEParts(hdrs, tp.R)
if err != nil {
t.Fatal("Could not parse the MIME parts recursively:", err)
}
htmlFound := false
imageFound := false
if expected, actual := 2, len(ps); actual != expected {
t.Error("Unexpected number of parts. Expected:", expected, "Was:", actual)
}
for _, part := range ps {
// part has "header" and "body []byte"
ct := part.header.Get("Content-Type")
if strings.Contains(ct, "image/png") {
imageFound = true
}
if strings.Contains(ct, "text/html") {
htmlFound = true
}
}
if !htmlFound {
t.Error("Did not find HTML part.")
}
if !imageFound {
t.Error("Did not find image part.")
}
}
func TestEmailHTML(t *testing.T) {
e := prepareEmail()
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, _, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatalf("Content-type header is invalid: %#v", ct)
} else if mt != "text/html" {
t.Fatalf("Content-type expected \"text/html\", not %v", mt)
}
}
func TestEmailTextAttachment(t *testing.T) {
e := prepareEmail()
e.Text = []byte("Text Body is, of course, supported!\n")
_, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, params, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "multipart/mixed" {
t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
}
b := params["boundary"]
if b == "" {
t.Fatalf("Invalid or missing boundary parameter: %#v", b)
}
if len(params) != 1 {
t.Fatal("Unexpected content-type parameters")
}
// Is the generated message parsable?
mixed := multipart.NewReader(msg.Body, params["boundary"])
text, err := mixed.NextPart()
if err != nil {
t.Fatalf("Could not find text component of email: %s", err)
}
// Does the text portion match what we expect?
mt, _, err = mime.ParseMediaType(text.Header.Get("Content-type"))
if err != nil {
t.Fatal("Could not parse message's Content-Type")
} else if mt != "text/plain" {
t.Fatal("Message missing text/plain")
}
plainText, err := ioutil.ReadAll(text)
if err != nil {
t.Fatal("Could not read plain text component of message: ", err)
}
if !bytes.Equal(plainText, []byte("Text Body is, of course, supported!\r\n")) {
t.Fatalf("Plain text is broken: %#q", plainText)
}
// Check attachments.
_, err = mixed.NextPart()
if err != nil {
t.Fatalf("Could not find attachment component of email: %s", err)
}
if _, err = mixed.NextPart(); err != io.EOF {
t.Error("Expected only text and one attachment!")
}
}
func TestEmailTextHtmlAttachment(t *testing.T) {
e := prepareEmail()
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
_, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, params, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "multipart/mixed" {
t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
}
b := params["boundary"]
if b == "" {
t.Fatal("Unexpected empty boundary parameter")
}
if len(params) != 1 {
t.Fatal("Unexpected content-type parameters")
}
// Is the generated message parsable?
mixed := multipart.NewReader(msg.Body, params["boundary"])
text, err := mixed.NextPart()
if err != nil {
t.Fatalf("Could not find text component of email: %s", err)
}
// Does the text portion match what we expect?
mt, params, err = mime.ParseMediaType(text.Header.Get("Content-type"))
if err != nil {
t.Fatal("Could not parse message's Content-Type")
} else if mt != "multipart/alternative" {
t.Fatal("Message missing multipart/alternative")
}
mpReader := multipart.NewReader(text, params["boundary"])
part, err := mpReader.NextPart()
if err != nil {
t.Fatal("Could not read plain text component of message: ", err)
}
plainText, err := ioutil.ReadAll(part)
if err != nil {
t.Fatal("Could not read plain text component of message: ", err)
}
if !bytes.Equal(plainText, []byte("Text Body is, of course, supported!\r\n")) {
t.Fatalf("Plain text is broken: %#q", plainText)
}
// Check attachments.
_, err = mixed.NextPart()
if err != nil {
t.Fatalf("Could not find attachment component of email: %s", err)
}
if _, err = mixed.NextPart(); err != io.EOF {
t.Error("Expected only text and one attachment!")
}
}
func TestEmailAttachment(t *testing.T) {
e := prepareEmail()
_, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, params, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "multipart/mixed" {
t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
}
b := params["boundary"]
if b == "" {
t.Fatal("Unexpected empty boundary parameter")
}
if len(params) != 1 {
t.Fatal("Unexpected content-type parameters")
}
// Is the generated message parsable?
mixed := multipart.NewReader(msg.Body, params["boundary"])
// Check attachments.
a, err := mixed.NextPart()
if err != nil {
t.Fatalf("Could not find attachment component of email: %s", err)
}
if !strings.HasPrefix(a.Header.Get("Content-Disposition"), "attachment") {
t.Fatalf("Content disposition is not attachment: %s", a.Header.Get("Content-Disposition"))
}
if _, err = mixed.NextPart(); err != io.EOF {
t.Error("Expected only one attachment!")
}
}
func TestHeaderEncoding(t *testing.T) {
cases := []struct {
field string
have string
want string
}{
{
field: "From",
have: "Needs Encóding <[email protected]>, Only ASCII <[email protected]>",
want: "=?utf-8?q?Needs_Enc=C3=B3ding?= <[email protected]>, \"Only ASCII\" <[email protected]>\r\n",
},
{
field: "Reply-To",
have: "Needs Encóding <[email protected]>, Only ASCII <[email protected]>",
want: "=?utf-8?q?Needs_Enc=C3=B3ding?= <[email protected]>, \"Only ASCII\" <[email protected]>\r\n",
},
{
field: "To",
have: "Keith Moore <[email protected]>, Keld Jørn Simonsen <[email protected]>",
want: "\"Keith Moore\" <[email protected]>, =?utf-8?q?Keld_J=C3=B8rn_Simonsen?= <[email protected]>\r\n",
},
{
field: "Cc",
have: "Needs Encóding <[email protected]>, \"Test :)\" <test@localhost>",
want: "=?utf-8?q?Needs_Enc=C3=B3ding?= <[email protected]>, \"Test :)\" <test@localhost>\r\n",
},
{
field: "Subject",
have: "Subject with a 🐟",
want: "=?UTF-8?q?Subject_with_a_=F0=9F=90=9F?=\r\n",
},
{
field: "Subject",
have: "Subject with only ASCII",
want: "Subject with only ASCII\r\n",
},
}
buff := &bytes.Buffer{}
for _, c := range cases {
header := make(textproto.MIMEHeader)
header.Add(c.field, c.have)
buff.Reset()
headerToBytes(buff, header)
want := fmt.Sprintf("%s: %s", c.field, c.want)
got := buff.String()
if got != want {
t.Errorf("invalid utf-8 header encoding. \nwant:%#v\ngot: %#v", want, got)
}
}
}
func TestEmailFromReader(t *testing.T) {
ex := &Email{
Subject: "Test Subject",
To: []string{"Jordan Wright <[email protected]>", "[email protected]"},
From: "Jordan Wright <[email protected]>",
ReplyTo: []string{"Jordan Wright <[email protected]>"},
Cc: []string{"[email protected]", "Two <[email protected]>"},
Bcc: []string{"[email protected]", "Four <[email protected]>"},
Text: []byte("This is a test email with HTML Formatting. It also has very long lines so\nthat the content must be wrapped if using quoted-printable decoding.\n"),
HTML: []byte("<div dir=\"ltr\">This is a test email with <b>HTML Formatting.</b>\u00a0It also has very long lines so that the content must be wrapped if using quoted-printable decoding.</div>\n"),
}
raw := []byte(`
MIME-Version: 1.0
Subject: Test Subject
From: Jordan Wright <[email protected]>
Reply-To: Jordan Wright <[email protected]>
To: Jordan Wright <[email protected]>, [email protected]
Cc: [email protected], Two <[email protected]>
Bcc: [email protected], Four <[email protected]>
Content-Type: multipart/alternative; boundary=001a114fb3fc42fd6b051f834280
--001a114fb3fc42fd6b051f834280
Content-Type: text/plain; charset=UTF-8
This is a test email with HTML Formatting. It also has very long lines so
that the content must be wrapped if using quoted-printable decoding.
--001a114fb3fc42fd6b051f834280
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">This is a test email with <b>HTML Formatting.</b>=C2=A0It =
also has very long lines so that the content must be wrapped if using quote=
d-printable decoding.</div>
--001a114fb3fc42fd6b051f834280--`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if e.Subject != ex.Subject {
t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
}
if !bytes.Equal(e.Text, ex.Text) {
t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
}
if !bytes.Equal(e.HTML, ex.HTML) {
t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
}
if e.From != ex.From {
t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
}
if len(e.To) != len(ex.To) {
t.Fatalf("Incorrect number of \"To\" addresses: %v != %v", len(e.To), len(ex.To))
}
if e.To[0] != ex.To[0] {
t.Fatalf("Incorrect \"To[0]\": %#q != %#q", e.To[0], ex.To[0])
}
if e.To[1] != ex.To[1] {
t.Fatalf("Incorrect \"To[1]\": %#q != %#q", e.To[1], ex.To[1])
}
if len(e.Cc) != len(ex.Cc) {
t.Fatalf("Incorrect number of \"Cc\" addresses: %v != %v", len(e.Cc), len(ex.Cc))
}
if e.Cc[0] != ex.Cc[0] {
t.Fatalf("Incorrect \"Cc[0]\": %#q != %#q", e.Cc[0], ex.Cc[0])
}
if e.Cc[1] != ex.Cc[1] {
t.Fatalf("Incorrect \"Cc[1]\": %#q != %#q", e.Cc[1], ex.Cc[1])
}
if len(e.Bcc) != len(ex.Bcc) {
t.Fatalf("Incorrect number of \"Bcc\" addresses: %v != %v", len(e.Bcc), len(ex.Bcc))
}
if e.Bcc[0] != ex.Bcc[0] {
t.Fatalf("Incorrect \"Bcc[0]\": %#q != %#q", e.Cc[0], ex.Cc[0])
}
if e.Bcc[1] != ex.Bcc[1] {
t.Fatalf("Incorrect \"Bcc[1]\": %#q != %#q", e.Bcc[1], ex.Bcc[1])
}
if len(e.ReplyTo) != len(ex.ReplyTo) {
t.Fatalf("Incorrect number of \"Reply-To\" addresses: %v != %v", len(e.ReplyTo), len(ex.ReplyTo))
}
if e.ReplyTo[0] != ex.ReplyTo[0] {
t.Fatalf("Incorrect \"ReplyTo\": %#q != %#q", e.ReplyTo[0], ex.ReplyTo[0])
}
}
func TestNonAsciiEmailFromReader(t *testing.T) {
ex := &Email{
Subject: "Test Subject",
To: []string{"Anaïs <[email protected]>"},
ReplyTo: []string{"Anaïs <[email protected]>"},
Cc: []string{"Patrik Fältström <[email protected]>"},
From: "Mrs Valérie Dupont <[email protected]>",
Text: []byte("This is a test message!"),
}
raw := []byte(`
MIME-Version: 1.0
Subject: =?UTF-8?Q?Test Subject?=
From: Mrs =?ISO-8859-1?Q?Val=C3=A9rie=20Dupont?= <[email protected]>
To: =?utf-8?q?Ana=C3=AFs?= <[email protected]>
Reply-To: =?utf-8?q?Ana=C3=AFs?= <[email protected]>
Cc: =?ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?= <[email protected]>
Content-type: text/plain; charset=ISO-8859-1
This is a test message!`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if e.Subject != ex.Subject {
t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
}
if e.From != ex.From {
t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
}
if e.To[0] != ex.To[0] {
t.Fatalf("Incorrect \"To\": %#q != %#q", e.To, ex.To)
}
if e.ReplyTo[0] != ex.ReplyTo[0] {
t.Fatalf("Incorrect \"Reply-To\": %#q != %#q", e.ReplyTo, ex.ReplyTo)
}
if e.Cc[0] != ex.Cc[0] {
t.Fatalf("Incorrect \"Cc\": %#q != %#q", e.Cc, ex.Cc)
}
}
func TestNonMultipartEmailFromReader(t *testing.T) {
ex := &Email{
Text: []byte("This is a test message!"),
Subject: "Example Subject (no MIME Type)",
Headers: textproto.MIMEHeader{},
}
ex.Headers.Add("Content-Type", "text/plain; charset=us-ascii")
ex.Headers.Add("Message-ID", "<[email protected]>")
raw := []byte(`From: "Foo Bar" <[email protected]>
Content-Type: text/plain
Subject: Example Subject (no MIME Type)
Message-ID: <[email protected]>
This is a test message!`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if ex.Subject != e.Subject {
t.Errorf("Incorrect subject. %#q != %#q\n", ex.Subject, e.Subject)
}
if !bytes.Equal(ex.Text, e.Text) {
t.Errorf("Incorrect body. %#q != %#q\n", ex.Text, e.Text)
}
if ex.Headers.Get("Message-ID") != e.Headers.Get("Message-ID") {
t.Errorf("Incorrect message ID. %#q != %#q\n", ex.Headers.Get("Message-ID"), e.Headers.Get("Message-ID"))
}
}
func TestBase64EmailFromReader(t *testing.T) {
ex := &Email{
Subject: "Test Subject",
To: []string{"Jordan Wright <[email protected]>"},
From: "Jordan Wright <[email protected]>",
Text: []byte("This is a test email with HTML Formatting. It also has very long lines so that the content must be wrapped if using quoted-printable decoding."),
HTML: []byte("<div dir=\"ltr\">This is a test email with <b>HTML Formatting.</b>\u00a0It also has very long lines so that the content must be wrapped if using quoted-printable decoding.</div>\n"),
}
raw := []byte(`
MIME-Version: 1.0
Subject: Test Subject
From: Jordan Wright <[email protected]>
To: Jordan Wright <[email protected]>
Content-Type: multipart/alternative; boundary=001a114fb3fc42fd6b051f834280
--001a114fb3fc42fd6b051f834280
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
VGhpcyBpcyBhIHRlc3QgZW1haWwgd2l0aCBIVE1MIEZvcm1hdHRpbmcuIEl0IGFsc28gaGFzIHZl
cnkgbG9uZyBsaW5lcyBzbyB0aGF0IHRoZSBjb250ZW50IG11c3QgYmUgd3JhcHBlZCBpZiB1c2lu
ZyBxdW90ZWQtcHJpbnRhYmxlIGRlY29kaW5nLg==
--001a114fb3fc42fd6b051f834280
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">This is a test email with <b>HTML Formatting.</b>=C2=A0It =
also has very long lines so that the content must be wrapped if using quote=
d-printable decoding.</div>
--001a114fb3fc42fd6b051f834280--`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if e.Subject != ex.Subject {
t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
}
if !bytes.Equal(e.Text, ex.Text) {
t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
}
if !bytes.Equal(e.HTML, ex.HTML) {
t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
}
if e.From != ex.From {
t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
}
}
func TestAttachmentEmailFromReader(t *testing.T) {
ex := &Email{
Subject: "Test Subject",
To: []string{"Jordan Wright <[email protected]>"},
From: "Jordan Wright <[email protected]>",
Text: []byte("Simple text body"),
HTML: []byte("<div dir=\"ltr\">Simple HTML body</div>\n"),
}
a, err := ex.Attach(bytes.NewReader([]byte("Let's just pretend this is raw JPEG data.")), "cat.jpeg", "image/jpeg")
if err != nil {
t.Fatalf("Error attaching image %s", err.Error())
}
b, err := ex.Attach(bytes.NewReader([]byte("Let's just pretend this is raw JPEG data.")), "cat-inline.jpeg", "image/jpeg")
if err != nil {
t.Fatalf("Error attaching inline image %s", err.Error())
}
raw := []byte(`
From: Jordan Wright <[email protected]>
Date: Thu, 17 Oct 2019 08:55:37 +0100
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary=35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
To: Jordan Wright <[email protected]>
Subject: Test Subject
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
Content-Type: multipart/alternative;
boundary=b10ca5b1072908cceb667e8968d3af04503b7ab07d61c9f579c15b416d7c
--b10ca5b1072908cceb667e8968d3af04503b7ab07d61c9f579c15b416d7c
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8
Simple text body
--b10ca5b1072908cceb667e8968d3af04503b7ab07d61c9f579c15b416d7c
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=UTF-8
<div dir=3D"ltr">Simple HTML body</div>
--b10ca5b1072908cceb667e8968d3af04503b7ab07d61c9f579c15b416d7c--
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
Content-Disposition: attachment;
filename="cat.jpeg"
Content-Id: <cat.content-id>
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
TGV0J3MganVzdCBwcmV0ZW5kIHRoaXMgaXMgcmF3IEpQRUcgZGF0YS4=
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
Content-Disposition: inline;
filename="cat-inline.jpeg"
Content-Id: <cat-inline.content-id>
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
TGV0J3MganVzdCBwcmV0ZW5kIHRoaXMgaXMgcmF3IEpQRUcgZGF0YS4=
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7--`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if e.Subject != ex.Subject {
t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
}
if !bytes.Equal(e.Text, ex.Text) {
t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
}
if !bytes.Equal(e.HTML, ex.HTML) {
t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
}
if e.From != ex.From {
t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
}
if len(e.Attachments) != 2 {
t.Fatalf("Incorrect number of attachments %d != %d", len(e.Attachments), 1)
}
if e.Attachments[0].Filename != a.Filename {
t.Fatalf("Incorrect attachment filename %s != %s", e.Attachments[0].Filename, a.Filename)
}
if !bytes.Equal(e.Attachments[0].Content, a.Content) {
t.Fatalf("Incorrect attachment content %#q != %#q", e.Attachments[0].Content, a.Content)
}
if e.Attachments[0].Header != nil {
if e.Attachments[0].Header.Get("Content-Id") != "<cat.content-id>" {
t.Fatalf("Incorrect attachment header Content-Id %s != %s", e.Attachments[0].Header.Get("Content-Id"), "<cat.content-id>")
}
}
if e.Attachments[1].Filename != b.Filename {
t.Fatalf("Incorrect attachment filename %s != %s", e.Attachments[1].Filename, b.Filename)
}
if !bytes.Equal(e.Attachments[1].Content, b.Content) {
t.Fatalf("Incorrect attachment content %#q != %#q", e.Attachments[1].Content, b.Content)
}
if e.Attachments[1].Header != nil {
if e.Attachments[1].Header.Get("Content-Id") != "<cat-inline.content-id>" {
t.Fatalf("Incorrect attachment header Content-Id %s != %s", e.Attachments[1].Header.Get("Content-Id"), "<cat-inline.content-id>")
}
}
}
func ExampleGmail() {
e := NewEmail()
e.From = "Jordan Wright <[email protected]>"
e.To = []string{"[email protected]"}
e.Bcc = []string{"[email protected]"}
e.Cc = []string{"[email protected]"}
e.Subject = "Awesome Subject"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
}
func ExampleAttach() {
e := NewEmail()
e.AttachFile("test.txt")
}
func Test_base64Wrap(t *testing.T) {
file := "I'm a file long enough to force the function to wrap a\n" +
"couple of lines, but I stop short of the end of one line and\n" +
"have some padding dangling at the end."
encoded := "SSdtIGEgZmlsZSBsb25nIGVub3VnaCB0byBmb3JjZSB0aGUgZnVuY3Rpb24gdG8gd3JhcCBhCmNv\r\n" +
"dXBsZSBvZiBsaW5lcywgYnV0IEkgc3RvcCBzaG9ydCBvZiB0aGUgZW5kIG9mIG9uZSBsaW5lIGFu\r\n" +
"ZApoYXZlIHNvbWUgcGFkZGluZyBkYW5nbGluZyBhdCB0aGUgZW5kLg==\r\n"
var buf bytes.Buffer
base64Wrap(&buf, []byte(file))
if !bytes.Equal(buf.Bytes(), []byte(encoded)) {
t.Fatalf("Encoded file does not match expected: %#q != %#q", string(buf.Bytes()), encoded)
}
}
// *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
func Test_quotedPrintEncode(t *testing.T) {
var buf bytes.Buffer
text := []byte("Dear reader!\n\n" +
"This is a test email to try and capture some of the corner cases that exist within\n" +
"the quoted-printable encoding.\n" +
"There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n")
expected := []byte("Dear reader!\r\n\r\n" +
"This is a test email to try and capture some of the corner cases that exist=\r\n" +
" within\r\n" +
"the quoted-printable encoding.\r\n" +
"There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
"s so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
" a fish: =F0=9F=90=9F\r\n")
qp := quotedprintable.NewWriter(&buf)
if _, err := qp.Write(text); err != nil {
t.Fatal("quotePrintEncode: ", err)
}
if err := qp.Close(); err != nil {
t.Fatal("Error closing writer", err)
}
if b := buf.Bytes(); !bytes.Equal(b, expected) {
t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", b, expected)
}
}
func TestMultipartNoContentType(t *testing.T) {
raw := []byte(`From: Mikhail Gusarov <[email protected]>
References: <[email protected]>
Date: Wed, 18 Nov 2009 01:02:38 +0600
Message-ID: <[email protected]>
MIME-Version: 1.0
Subject: Re: [notmuch] Working with Maildir storage?
Content-Type: multipart/mixed; boundary="===============1958295626=="
--===============1958295626==
Content-Type: multipart/signed; boundary="=-=-=";
micalg=pgp-sha1; protocol="application/pgp-signature"
--=-=-=
Content-Transfer-Encoding: quoted-printable
Twas brillig at 14:00:54 17.11.2009 UTC-05 when [email protected] did g=
yre and gimble:
--=-=-=
Content-Type: application/pgp-signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iQIcBAEBAgAGBQJLAvNOAAoJEJ0g9lA+M4iIjLYQAKp0PXEgl3JMOEBisH52AsIK
=/ksP
-----END PGP SIGNATURE-----
--=-=-=--
--===============1958295626==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Testing!
--===============1958295626==--
`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error when parsing email %s", err.Error())
}
if !bytes.Equal(e.Text, []byte("Testing!")) {
t.Fatalf("Error incorrect text: %#q != %#q\n", e.Text, "Testing!")
}
}
func TestNoMultipartHTMLContentTypeBase64Encoding(t *testing.T) {
raw := []byte(`MIME-Version: 1.0
From: [email protected]
Date: 7 Jan 2021 03:07:44 -0800
Subject: Hello
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
Message-Id: <[email protected]>
PGh0bWw+PGhlYWQ+PHRpdGxlPnRlc3Q8L3RpdGxlPjwvaGVhZD48Ym9keT5IZWxsbyB3
b3JsZCE8L2JvZHk+PC9odG1sPg==
`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error when parsing email %s", err.Error())
}
if !bytes.Equal(e.HTML, []byte("<html><head><title>test</title></head><body>Hello world!</body></html>")) {
t.Fatalf("Error incorrect text: %#q != %#q\n", e.Text, "<html>...</html>")
}
}
// *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
func Test_quotedPrintDecode(t *testing.T) {
text := []byte("Dear reader!\r\n\r\n" +
"This is a test email to try and capture some of the corner cases that exist=\r\n" +
" within\r\n" +
"the quoted-printable encoding.\r\n" +
"There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
"s so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
" a fish: =F0=9F=90=9F\r\n")
expected := []byte("Dear reader!\r\n\r\n" +
"This is a test email to try and capture some of the corner cases that exist within\r\n" +
"the quoted-printable encoding.\r\n" +
"There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\r\n")
qp := quotedprintable.NewReader(bytes.NewReader(text))
got, err := ioutil.ReadAll(qp)
if err != nil {
t.Fatal("quotePrintDecode: ", err)
}
if !bytes.Equal(got, expected) {
t.Errorf("quotedPrintDecode generated incorrect results: %#q != %#q", got, expected)
}
}
func Benchmark_base64Wrap(b *testing.B) {
// Reasonable base case; 128K random bytes
file := make([]byte, 128*1024)
if _, err := rand.Read(file); err != nil {
panic(err)
}
for i := 0; i <= b.N; i++ {
base64Wrap(ioutil.Discard, file)
}
}
func TestParseSender(t *testing.T) {
var cases = []struct {
e Email
want string
haserr bool
}{
{
Email{From: "[email protected]"},
false,
},
{
Email{Sender: "[email protected]", From: "[email protected]"},
false,
},
{
Email{Sender: "bad_address_sender"},
"",
true,
},
{
Email{Sender: "[email protected]", From: "bad_address_from"},
false,
},
}
for i, testcase := range cases {
got, err := testcase.e.parseSender()
if got != testcase.want || (err != nil) != testcase.haserr {
t.Errorf(`%d: got %s != want %s or error "%t" != "%t"`, i+1, got, testcase.want, err != nil, testcase.haserr)
}
}
}