forked from DockYard/elixir-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfc_2822_test.exs
1021 lines (832 loc) · 35.8 KB
/
rfc_2822_test.exs
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Mail.Parsers.RFC2822Test do
use ExUnit.Case, async: true
doctest Mail.Parsers.RFC2822
test "parses a singlepart message" do
message =
parse_email("""
From: [email protected]
Reply-To: [email protected]
Subject: Test Email
Content-Type: text/plain; foo=bar;
baz=qux;
This is the body!
It has more than one line
""")
assert message.headers["to"] == ["[email protected]"]
assert message.headers["from"] == "[email protected]"
assert message.headers["reply-to"] == "[email protected]"
assert message.headers["subject"] == "Test Email"
assert message.headers["content-type"] == ["text/plain", {"foo", "bar"}, {"baz", "qux"}]
assert message.body == "This is the body!\r\nIt has more than one line"
end
test "parses a singlepart message with no body" do
message =
parse_email("""
From: [email protected]
Reply-To: [email protected]
Subject: Test Email
""")
assert message.headers["to"] == ["[email protected]"]
assert message.headers["from"] == "[email protected]"
assert message.headers["reply-to"] == "[email protected]"
assert message.headers["subject"] == "Test Email"
assert message.body == nil
end
test "parses a multipart message" do
message =
parse_email("""
To: Test User <[email protected]>, Other User <[email protected]>
CC: The Dude <[email protected]>, Batman <[email protected]>
From: Me <[email protected]>
Reply-To: OtherMe <[email protected]>
Subject: Test email
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=foobar
--foobar
Content-Type: text/plain
This is some text
--foobar
Content-Type: text/html
<h1>This is some HTML</h1>
--foobar--
""")
assert message.headers["to"] == [
{"Test User", "[email protected]"},
{"Other User", "[email protected]"}
]
assert message.headers["cc"] == [
{"The Dude", "[email protected]"},
{"Batman", "[email protected]"}
]
assert message.headers["from"] == {"Me", "[email protected]"}
assert message.headers["reply-to"] == {"OtherMe", "[email protected]"}
assert message.headers["content-type"] == ["multipart/alternative", {"boundary", "foobar"}]
[text_part, html_part] = message.parts
assert text_part.headers["content-type"] == ["text/plain", {"charset", "us-ascii"}]
assert text_part.body == "This is some text"
assert html_part.headers["content-type"] == ["text/html", {"charset", "us-ascii"}]
assert html_part.body == "<h1>This is some HTML</h1>"
end
test "parses a multipart message with a body" do
message =
parse_email("""
To: Test User <[email protected]>, Other User <[email protected]>
CC: The Dude <[email protected]>, Batman <[email protected]>
From: Me <[email protected]>
Subject: Test email
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=foobar
This is a multi-part message in MIME format
--foobar
Content-Type: text/plain
This is some text
--foobar
Content-Type: text/html
<h1>This is some HTML</h1>
--foobar
x-my-header: no body!
--foobar--
""")
assert message.body == nil
[text_part, html_part, headers_only_part] = message.parts
assert text_part.headers["content-type"] == ["text/plain", {"charset", "us-ascii"}]
assert text_part.body == "This is some text"
assert html_part.headers["content-type"] == ["text/html", {"charset", "us-ascii"}]
assert html_part.body == "<h1>This is some HTML</h1>"
assert headers_only_part.headers["x-my-header"] == "no body!"
assert headers_only_part.body == nil
end
# A reproduction of an email found in the wild.
test "parses a multipart message with a body with two html parts" do
message =
parse_email("""
To: Test User <[email protected]>, Other User <[email protected]>
CC: The Dude <[email protected]>, Batman <[email protected]>
From: Me <[email protected]>
Subject: Test email
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=Apple-Mail-358A6BE7-EE99-47E3-B9DE-7575E1C181D1
Content-Transfer-Encoding: 7bit
--Apple-Mail-358A6BE7-EE99-47E3-B9DE-7575E1C181D1
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Text part
--Apple-Mail-358A6BE7-EE99-47E3-B9DE-7575E1C181D1
Content-Type: multipart/mixed;
boundary=Apple-Mail-3B120747-147B-46E8-B375-DE9974BB35B1
Content-Transfer-Encoding: 7bit
--Apple-Mail-3B120747-147B-46E8-B375-DE9974BB35B1
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<html></html>
--Apple-Mail-3B120747-147B-46E8-B375-DE9974BB35B1
Content-Type: application/pdf; name=Payment.pdf; x-apple-part-url=45B14A08-AD6D-4298-9E0D-D71498E7112E
Content-Disposition: inline; filename=Payment.pdf
Content-Transfer-Encoding: base64
JVBERi0xLjcKJeLjz9MKNiAwIG9iago8PCAvQ3JlYXRvciAoT3BlblRleHQgRXhzdHJlYW0gVmVy
--Apple-Mail-3B120747-147B-46E8-B375-DE9974BB35B1
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<html>Non empty part</html>
--Apple-Mail-3B120747-147B-46E8-B375-DE9974BB35B1--
--Apple-Mail-358A6BE7-EE99-47E3-B9DE-7575E1C181D1--
""")
assert message.body == nil
[text_part, html_part] = message.parts
assert text_part.headers["content-type"] == ["text/plain", {"charset", "utf-8"}]
assert text_part.body == "Text part"
assert html_part.headers["content-type"] == [
"multipart/mixed",
{"boundary", "Apple-Mail-3B120747-147B-46E8-B375-DE9974BB35B1"}
]
[html_part, _pdf_part, html_part2] = html_part.parts
assert html_part.headers["content-type"] == ["text/html", {"charset", "us-ascii"}]
assert html_part.body == "<html></html>"
assert html_part2.body == "<html>Non empty part</html>"
assert Mail.get_html(message) == html_part2
end
test "to_datetime/1" do
import Mail.Parsers.RFC2822, only: [to_datetime: 1]
assert to_datetime("Fri, 1 Jan 2016 00:00:00 +0000") == ~U"2016-01-01 00:00:00Z"
assert to_datetime("1 Feb 2016 01:02:03 +0000") == ~U"2016-02-01 01:02:03Z"
assert to_datetime(" 1 Mar 2016 11:12:13 +0000") == ~U"2016-03-01 11:12:13Z"
assert to_datetime("\t1 Apr 2016 22:33:44 +0000") == ~U"2016-04-01 22:33:44Z"
assert to_datetime("12 Jan 2016 00:00:00 +0000") == ~U"2016-01-12 00:00:00Z"
assert to_datetime("25 Dec 2016 00:00:00 +0000 (UTC)") == ~U"2016-12-25 00:00:00Z"
# The spec specifies that the seconds are optional
assert to_datetime("14 Jun 2019 11:24 +0000") == ~U"2019-06-14 11:24:00Z"
assert to_datetime("28 JUN 2021 09:10 +0200") == ~U"2021-06-28 07:10:00Z"
assert to_datetime("12 May 2020 12:08:24 UT") == ~U"2020-05-12 12:08:24Z"
end
# Handle obsolute date time https://datatracker.ietf.org/doc/html/rfc2822#section-4.3
test "to_datetime/1 with obsolete information" do
import Mail.Parsers.RFC2822, only: [to_datetime: 1]
# Two-digit year
assert to_datetime("12 May 50 12:08:24 +0000") == ~U"1950-05-12 12:08:24Z"
assert to_datetime("12 May 49 12:08:24 +0000") == ~U"2049-05-12 12:08:24Z"
# Digit month
assert to_datetime("01 08 2023 08:59:03 +0000") == ~U"2023-08-01 08:59:03Z"
# Obsolete time zones
assert to_datetime("01 Aug 2023 08:59:03 UT") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("03 Apr 2017 12:30:55 UTC") == ~U"2017-04-03 12:30:55Z"
assert to_datetime("01 Aug 2023 08:59:03 GMT") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 EDT") == ~U"2023-08-01 12:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 EST") == ~U"2023-08-01 13:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 CDT") == ~U"2023-08-01 13:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 CST") == ~U"2023-08-01 14:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 MDT") == ~U"2023-08-01 14:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 MST") == ~U"2023-08-01 15:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 PDT") == ~U"2023-08-01 15:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 PST") == ~U"2023-08-01 16:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 A") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 B") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 C") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 D") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 E") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 F") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 G") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 H") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 I") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 K") == ~U"2023-08-01 08:59:03Z"
assert to_datetime("01 Aug 2023 08:59:03 Z") == ~U"2023-08-01 08:59:03Z"
end
test "to_datetime/1 with invalid RFC2822 timestamps (found in the wild)" do
import Mail.Parsers.RFC2822, only: [to_datetime: 1]
assert to_datetime("Thu, 16 May 2019 5:50:53 +0700") == ~U"2019-05-15 22:50:53Z"
assert to_datetime("Tue May 30 15:29:15 2017") == ~U"2017-05-30 15:29:15Z"
assert to_datetime("Tue May 3 15:29:15 2017") == ~U"2017-05-03 15:29:15Z"
assert to_datetime("Wed, 14 05 2015 12:34:17") == ~U"2015-05-14 12:34:17Z"
assert to_datetime("Tue, 20 Jun 2017 09:44:58.568 +0000 (UTC)") == ~U"2017-06-20 09:44:58Z"
assert to_datetime("Fri Apr 15 17:22:55 CAT 2016") == ~U"2016-04-15 17:22:55Z"
assert to_datetime("Tue, 05 Nov 2024 10:31:43 MSK") == ~U"2024-11-05 10:31:43Z"
[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
|> Enum.with_index(1)
|> Enum.each(fn {long_month, idx} ->
idx =
if idx < 10 do
"0#{idx}"
else
idx
end
{:ok, datetime, 0} = DateTime.from_iso8601("2024-#{idx}-13 18:29:58Z")
assert to_datetime("13 #{long_month} 2024 18:29:58 +0000") == datetime
end)
end
test "to_datetime/1 when the timestamp is unsupported" do
import Mail.Parsers.RFC2822, only: [to_datetime: 1]
assert to_datetime("invalid date string") == {:error, "invalid date string"}
end
test "parse_recipient_value retrieves a list of name and addresses" do
recipient =
"The Dude <[email protected]>, [email protected], super<[email protected]>, \"[email protected]\" <[email protected]>"
retrieved_recipients = [
{"The Dude", "[email protected]"},
{"super", "[email protected]"},
]
assert parse_recipient(recipient) == retrieved_recipients
end
test "parse_recipient_value retrieves an empty list when recipient is empty" do
assert parse_recipient("") == []
end
test "parse_recipient_value retrieves an empty list when no \"address\" found" do
assert parse_recipient("NoEmail") == []
end
test "parse_recipient_value retrieves a list when only one \"address\" found" do
assert parse_recipient("[email protected]") == ["[email protected]"]
end
test "parses a nested multipart message with encoded part" do
message =
parse_email("""
To: Test User <[email protected]>, Other User <[email protected]>
CC: The Dude <[email protected]>, Batman <[email protected]>
From: Me <[email protected]>
Content-Type: multipart/mixed; boundary="foobar"
Date: Fri, 1 Jan
2016 00:00:00 +0000
--foobar
Content-Type: multipart/alternative; boundary="bazqux"
--bazqux
Content-Type: text/plain
This is some text
--bazqux
Content-Type: text/html
<h1>This is the HTML</h1>
--bazqux--
--foobar
Content-Type: text/markdown
Content-Disposition: attachment; filename=README.md
Content-Transfer-Encoding: base64
SGVsbG8gd29ybGQh
--foobar--
""")
assert message.headers["to"] == [
{"Test User", "[email protected]"},
{"Other User", "[email protected]"}
]
assert message.headers["cc"] == [
{"The Dude", "[email protected]"},
{"Batman", "[email protected]"}
]
assert message.headers["from"] == {"Me", "[email protected]"}
assert message.headers["content-type"] == ["multipart/mixed", {"boundary", "foobar"}]
assert message.headers["date"] == ~U"2016-01-01 00:00:00Z"
[alt_part, attach_part] = message.parts
assert alt_part.headers["content-type"] == ["multipart/alternative", {"boundary", "bazqux"}]
[text_part, html_part] = alt_part.parts
assert text_part.headers["content-type"] == ["text/plain", {"charset", "us-ascii"}]
assert text_part.body == "This is some text"
assert html_part.headers["content-type"] == ["text/html", {"charset", "us-ascii"}]
assert html_part.body == "<h1>This is the HTML</h1>"
assert attach_part.headers["content-type"] == ["text/markdown", {"charset", "us-ascii"}]
assert attach_part.headers["content-disposition"] == ["attachment", {"filename", "README.md"}]
assert attach_part.headers["content-transfer-encoding"] == "base64"
assert attach_part.body == "Hello world!"
end
test "parses unstructured headers" do
message =
parse_email("""
Delivered-To: [email protected]
Received: by 101.102.103.104 with SMTP id abcdefg;
Fri, 1 Apr 2016 11:08:31 -0700 (PDT)
X-Received: 201.202.203.204 with SMTP id abcdefg.12.123456;
Fri, 01 Apr 2016 11:08:31 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=example.com; s=20160922;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to;
bh=ABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABC=;
b=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890+/
abcd==
Return-Path: <[email protected]>
To: Test User <[email protected]>, Other User <[email protected]>
CC: The Dude <[email protected]>, Batman <[email protected]>
From: Me <[email protected]>
Content-Type: text/plain
Date: Fri, 1 Jan 2016 00:00:00 +0000
Test
""")
assert message.headers["delivered-to"] == "[email protected]"
assert message.headers["received"] == [
[
"by 101.102.103.104 with SMTP id abcdefg",
{"date", ~U"2016-04-01 18:08:31Z"}
]
]
assert message.headers["x-received"] ==
"201.202.203.204 with SMTP id abcdefg.12.123456; Fri, 01 Apr 2016 11:08:31 -0700 (PDT)"
assert message.headers["dkim-signature"] ==
"v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com; s=20160922; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=ABCDEFGHABCDEFGHABCDEFGHABCDEFGHABCDEFGHABC=; b=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890+/ abcd=="
end
test "parses more than 1 'Received:' header" do
message =
parse_email("""
Received: from mail3.example.tld ([10.20.30.40]) by mail.fake.tld ([10.10.10.10]);
Fri, 1 Apr 2016 11:09:07 -0700 (PDT)
Received: from mail.fake.tld ([10.10.10.10]) by localhost ([127.0.0.1]);
Fri, 1 Apr 2016 11:08:35 -0700 (PDT)
Received: from localhost ([127.0.0.1]) by localhost (MyMailSoftware);
Fri, 1 Apr 2016 11:08:31 -0700 (PDT)
From: [email protected]
Subject: Test Email
Content-Type: text/plain
Test
""")
assert message.headers["received"] == [
[
"from localhost ([127.0.0.1]) by localhost (MyMailSoftware)",
{"date", ~U"2016-04-01 18:08:31Z"}
],
[
"from mail.fake.tld ([10.10.10.10]) by localhost ([127.0.0.1])",
{"date", ~U"2016-04-01 18:08:35Z"}
],
[
"from mail3.example.tld ([10.20.30.40]) by mail.fake.tld ([10.10.10.10])",
{"date", ~U"2016-04-01 18:09:07Z"}
]
]
end
test "parses with a '=' in boundary" do
message =
parse_email("""
To: Test User <[email protected]>, Other User <[email protected]>
CC: The Dude <[email protected]>, Batman <[email protected]>
From: Me <[email protected]>
Date: Fri, 1 Jan 2016 00:00:00 +0000
Content-Type: multipart/mixed;
boundary="----=_Part_295474_20544590.1456382229928"
------=_Part_295474_20544590.1456382229928
Content-Type: text/plain
This is some text
------=_Part_295474_20544590.1456382229928
Content-Type: text/html
<h1>This is some HTML</h1>
------=_Part_295474_20544590.1456382229928--
""")
assert message.headers["content-type"] == [
"multipart/mixed",
{"boundary", "----=_Part_295474_20544590.1456382229928"}
]
end
test "parse long, wrapped header" do
message =
parse_email("""
X-ReallyLongHeaderNameThatCausesBodyToWrap:
\tBodyOnNewLine
Body
""")
assert message.headers["x-reallylongheadernamethatcausesbodytowrap"] == "BodyOnNewLine"
end
test "parse header with folded encoded word" do
# This is seen in the examples in https://www.rfc-editor.org/rfc/rfc2047.html#section-8
message =
parse_email("""
X-Encoded-Word: =?utf-8?Q?h=CE=B5=C5=82=C5=82=C3=B8=20w=C3=B8?=
=?utf-8?Q?r=C5=82d?=
Body
""")
assert message.headers["x-encoded-word"] == "hεłłø wørłd"
end
test "allow empty body (RFC2822 §3.5)" do
message =
parse_email("""
To: Test User <[email protected]>
From: Me <[email protected]>
Date: Fri, 1 Jan 2016 00:00:00 +0000
Subject: Blank body
""")
assert message.body == ""
end
test "address comment parsing" do
message =
parse_email("""
To: Test User <[email protected]> (comment)
CC: [email protected] (comment)
From: <[email protected]>
Date: Fri, 1 Jan 2016 00:00:00 +0000
Subject: Blank body
""")
assert message.headers["to"] == [{"Test User", "[email protected]"}]
assert message.headers["cc"] == ["[email protected]"]
assert message.headers["from"] == "[email protected]"
end
test "address name contains comma" do
message =
parse_email("""
To: "User, Test" <[email protected]>
From: "Lastname, First Names" <[email protected]>
Date: Fri, 1 Jan 2016 00:00:00 +0000
Subject: Blank body
""")
assert message.headers["to"] == [{"User, Test", "[email protected]"}]
assert message.headers["cc"] == [
{"User, First", "[email protected]"},
{"User, Second", "[email protected]"},
]
assert message.headers["from"] == {"Lastname, First Names", "[email protected]"}
end
# See https://tools.ietf.org/html/rfc2047
test "parses headers with encoded word syntax" do
message =
parse_email("""
From: [email protected]
Subject: =?utf-8?Q?=C2=A3?=200.00=?UTF-8?q?_=F0=9F=92=B5?=
Content-Type: multipart/mixed;
boundary="----=_Part_295474_20544590.1456382229928"
------=_Part_295474_20544590.1456382229928
Content-Type: text/plain
This is some text
------=_Part_295474_20544590.1456382229928
Content-Type: image/png
Content-Disposition: attachment; filename=Emoji =?utf-8?B?8J+YgA==?= Filename.png
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==
------=_Part_295474_20544590.1456382229928--
""")
assert message.headers["subject"] == "£200.00 💵"
[_, part] = message.parts
assert ["attachment", {"filename", "Emoji 😀 Filename.png"}] =
part.headers["content-disposition"]
end
test "parses headers with non-encoded string that looks like encoded word syntax" do
message =
parse_email("""
From: [email protected]
Subject: Subject that has =? for an unknown reason with =?utf-8?B?8J+YgA==?=
List-Unsubscribe: https://some-domain.com/te/c/abcdef=?signature=abcdef
Content-Type: multipart/mixed;
boundary="----=_Part_295474_20544590.1456382229928"
------=_Part_295474_20544590.1456382229928
Content-Type: text/plain
This is some text
------=_Part_295474_20544590.1456382229928--
""")
assert message.headers["subject"] == "Subject that has =? for an unknown reason with 😀"
assert message.headers["list-unsubscribe"] ==
"https://some-domain.com/te/c/abcdef=?signature=abcdef"
end
test "parses structured header with extraneous semicolon" do
message =
parse_email("""
From: [email protected]
Subject: Test
Content-Type: text/plain;
charset=utf-8;
This is some text
""")
assert message.body == "This is some text"
end
test "parse invalid Received header" do
message =
parse_email("""
Received: by 2002:a81:578e:0:0:0:0:0 with SMTP id l136csp2273163ywb;
Sat, 22 Jun 2019 17:59:49 -0700 (PDT)
Received: by filter0419p1iad2.sendgrid.net with SMTP id filter0419p1iad2-17662-5D0ECF02-32
2019-06-23 00:59:46.828888551 +0000 UTC m=+266323.963383415
From: [email protected]
Subject: Test
Body
""")
assert message.headers["received"] == [
[
"by filter0419p1iad2.sendgrid.net with SMTP id filter0419p1iad2-17662-5D0ECF02-32 2019-06-23 00:59:46.828888551 +0000 UTC m=+266323.963383415"
],
[
"by 2002:a81:578e:0:0:0:0:0 with SMTP id l136csp2273163ywb",
{"date", ~U"2019-06-23 00:59:49Z"}
]
]
end
test "parse received header with wrapped date" do
message =
parse_email("""
Received: from EUR01-HE1-obe.outbound.protection.outlook.com
(213.199.154.213) by us1.smtp.exclaimer.net (191.237.4.149) with Exclaimer
Signature Manager ESMTP Proxy us1.smtp.exclaimer.net; Wed, 1 Aug 2018
09:49:43 +0000
Received: from EUR01-HE1-obe.outbound.protection.outlook.com
(213.199.154.208) by us1.smtp.exclaimer.net (191.237.4.149) with Exclaimer
Signature Manager ESMTP Proxy us1.smtp.exclaimer.net; Mon, 6 Aug 2018
07:23:18 +0000
""")
assert message.headers["received"] == [
[
"from EUR01-HE1-obe.outbound.protection.outlook.com\t (213.199.154.208) by us1.smtp.exclaimer.net (191.237.4.149) with Exclaimer\t Signature Manager ESMTP Proxy us1.smtp.exclaimer.net",
{"date", ~U"2018-08-06 07:23:18Z"}
],
[
"from EUR01-HE1-obe.outbound.protection.outlook.com (213.199.154.213) by us1.smtp.exclaimer.net (191.237.4.149) with Exclaimer Signature Manager ESMTP Proxy us1.smtp.exclaimer.net",
{"date", ~U"2018-08-01 09:49:43Z"}
]
]
end
test "parse invalid date in Received header" do
message =
parse_email("""
Received: from local-ip[x.x.x.x] by FTGS; 28-Dec-2014 20:04:31 +0200
Received: from trusted client by mx4.sika.com; Tue May 30 15:29:15 2017
Received: from freshdesk.com (ec2-x-x-x-x.compute-1.amazonaws.com [x.x.x.x])
by x.sendgrid.net (SG) with ESMTP id eSJywaprRzabHWQplQP8xw
for <[email protected]>; Tue, 20 Jun 2017 09:44:58.568 +0000 (UTC)
Received: from ip<x.x.x.> ([x.x.x.x])
by zm-as2 with ESMTP id fd672312-a36d-4bfe-8770-01b5cb3baca4 for [email protected];
Tue Aug 8 12:05:31 2017
Received: from [email protected] with Spamsniper 2.96.32 (Processed in 1.059114 secs);
Received: from x.x.x.x
by Spam Quarantine V01-06377SMG01.x.x.x (x.x.x.x) for <[email protected]>; Fri Apr 15 17:22:55 CAT 2016
Received: from w.x.y.z ([1.1.1.1]) by x.y.local with InterScan Messaging Security Suite; Mon, 25 Nov 2019\u0020\r\n\t13:00:46 +0200
""")
assert message.headers["received"] == [
[
"from w.x.y.z ([1.1.1.1]) by x.y.local with InterScan Messaging Security Suite",
{"date", ~U"2019-11-25 11:00:46Z"}
],
[
"from x.x.x.x\tby Spam Quarantine V01-06377SMG01.x.x.x (x.x.x.x) for <[email protected]>",
{"date", ~U"2016-04-15 17:22:55Z"}
],
["from [email protected] with Spamsniper 2.96.32 (Processed in 1.059114 secs)"],
[
"from ip<x.x.x.> ([x.x.x.x])\tby zm-as2 with ESMTP id fd672312-a36d-4bfe-8770-01b5cb3baca4 for [email protected]",
{"date", ~U"2017-08-08 12:05:31Z"}
],
[
"from freshdesk.com (ec2-x-x-x-x.compute-1.amazonaws.com [x.x.x.x])\tby x.sendgrid.net (SG) with ESMTP id eSJywaprRzabHWQplQP8xw\tfor <[email protected]>",
{"date", ~U"2017-06-20 09:44:58Z"}
],
["from trusted client by mx4.sika.com", {"date", ~U"2017-05-30 15:29:15Z"}],
["from local-ip[x.x.x.x] by FTGS", {"date", ~U"2014-12-28 18:04:31Z"}]
]
end
test "parse date in date header" do
message =
parse_email("""
Date: Wed, 14 05 2015 12:34:17
""")
assert message.headers["date"] == ~U"2015-05-14 12:34:17Z"
end
test "handle comment after semi-colon in received header value" do
message =
parse_email("""
Received: from smtp.notes.na.collabserv.com (192.155.248.91)
by d50lp03.ny.us.ibm.com (158.87.18.22) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted;
(version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128/128)
Thu, 8 Jun 2017 04:22:53 -0400
""")
assert message.headers["received"] == [
[
"from smtp.notes.na.collabserv.com (192.155.248.91)\tby d50lp03.ny.us.ibm.com (158.87.18.22) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128/128)",
{"date", ~U"2017-06-08 08:22:53Z"}
]
]
end
test "parses quoted string filename with embedded semi-colon (RFC2822 §3.2.5)" do
message =
parse_email("""
From: [email protected]
Subject: Test
Content-Type: multipart/mixed;
boundary="----=_Part_295474_20544590.1456382229928"
------=_Part_295474_20544590.1456382229928
Content-Type: text/plain
This is some text
------=_Part_295474_20544590.1456382229928
Content-Type: image/gif;
charset="UTF-8";
name="★.gif"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="★.gif"
Content-Id: <5cf3f485fe9b1d93040fac887e133234.gif>
X-Attachment-Id: <5cf3f485fe9b1d93040fac887e133234.gif>
R0lGODlhOw==\r\n"
------=_Part_295474_20544590.1456382229928--
""")
[_, part] = message.parts
assert ["inline", {"filename", "★.gif"}] = part.headers["content-disposition"]
assert ["image/gif", {"charset", "UTF-8"}, {"name", "★.gif"}] =
part.headers["content-type"]
end
test "parses Windows-1252 encoded filenames" do
email = """
From: [email protected]
Subject: Test
Content-Type: multipart/mixed;
boundary="----=_Part_295474_20544590.1456382229928"
------=_Part_295474_20544590.1456382229928
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
fran=E7aise pr=E8s =E0 th=E9=E2tre lumi=E8re
------=_Part_295474_20544590.1456382229928
Content-Type: application/octet-stream;
name="=?Windows-1252?Q?Imagin=E9.pdf?="
Content-Description: =?Windows-1252?Q?Imagine=E9.pdf?=
Content-Disposition: attachment;
filename="=?Windows-1252?Q?Imagine=E9.pdf?="; size=864872;
creation-date="Tue, 08 Oct 2024 14:16:59 GMT";
modification-date="Tue, 08 Oct 2024 14:16:59 GMT"
Content-Transfer-Encoding: base64
JVBERi0xLjcKJeLjz9MKNiAwIG9iago8PCAvQ3JlYXRvciAoT3BlblRleHQgRXhzdHJlYW0gVmVy
------=_Part_295474_20544590.1456382229928
Content-Type: application/pdf;
name="=?windows-1258?Q?Pre=ECsentation.pdf?="
Content-Description: =?windows-1258?Q?Pre=ECsentation.pdf?=
Content-Disposition: attachment;
filename="=?windows-1258?Q?Pre=ECsentation.pdf?="; size=3827236;
creation-date="Wed, 11 Sep 2024 09:27:41 GMT";
modification-date="Wed, 09 Oct 2024 08:27:14 GMT"
Content-ID: <f_m0xno2c63>
Content-Transfer-Encoding: base64
JVBERi0xLjcKJeLjz9MKNiAwIG9iago8PCAvQ3JlYXRvciAoT3BlblRleHQgRXhzdHJlYW0gVmVy
------=_Part_295474_20544590.1456382229928
Content-Type: application/octet-stream;
name="=?Windows-1252?Q?ID_S=E9_-_Liste_inscrits.xlsx?="
Content-Description: =?Windows-1252?Q?ID_S=E9_-_Liste_inscrits.xlsx?=
Content-Disposition: attachment;
filename="=?Windows-1252?Q?ID_S=E9_-_Liste_inscrits.xlsx?=";
size=19791; creation-date="Tue, 08 Oct 2024 14:16:55 GMT";
modification-date="Tue, 08 Oct 2024 14:16:55 GMT"
Content-Transfer-Encoding: base64
JVBERi0xLjcKJeLjz9MKNiAwIG9iago8PCAvQ3JlYXRvciAoT3BlblRleHQgRXhzdHJlYW0gVmVy
------=_Part_295474_20544590.1456382229928
"""
message = parse_email(email)
assert [part1, part2, part3, part4] = message.parts
assert %{headers: %{"content-type" => ["text/plain" | _]}} = part1
assert part1.body == "fran\xE7aise pr\xE8s \xE0 th\xE9\xE2tre lumi\xE8re"
assert %{
headers: %{
"content-type" => ["application/octet-stream", {"name", "Imagin\xE9.pdf"}]
}
} = part2
assert %{headers: %{"content-type" => ["application/pdf", {"name", "Pre\xECsentation.pdf"}]}} =
part3
assert %{
headers: %{
"content-type" => [
"application/octet-stream",
{"name", "ID S\xE9 - Liste inscrits.xlsx"}
]
}
} = part4
# This is a simple character replacement function that simulates charset change from Windows-1252/1258 to UTF-8
message =
parse_email(email,
charset_handler: fn _charset, string ->
string
|> String.graphemes()
|> Enum.map(fn
# Windows-1252
<<0xE0>> -> "à"
<<0xE2>> -> "â"
<<0xE7>> -> "ç"
<<0xE8>> -> "è"
<<0xE9>> -> "é"
# Windows-1258
<<0xEC>> -> "\u0301"
char -> char
end)
|> Enum.join()
end
)
assert [part1, part2, part3, part4] = message.parts
assert %{headers: %{"content-type" => ["text/plain" | _]}} = part1
assert part1.body == "française près à théâtre lumière"
assert %{
headers: %{
"content-type" => ["application/octet-stream", {"name", "Imaginé.pdf"}]
}
} = part2
assert %{headers: %{"content-type" => ["application/pdf", {"name", "Présentation.pdf"}]}} =
part3
assert %{
headers: %{
"content-type" => [
"application/octet-stream",
{"name", "ID Sé - Liste inscrits.xlsx"}
]
}
} = part4
end
test "content-type mixed with no body" do
message =
parse_email("""
From: [email protected]
Subject: Test
Content-Type: multipart/mixed;
boundary="----=_Part_295474_20544590.1456382229928"
""")
assert message.parts == []
assert message.body == nil
end
test "content-type with explicit charset" do
message =
parse_email("""
Content-Type: text/html; charset=us-ascii
""")
assert message.headers["content-type"] == ["text/html", {"charset", "us-ascii"}]
end
test "content-type with atypical casing" do
message =
parse_email("""
Content-type: text/html; charset=us-ascii
""")
assert message.headers["content-type"] == ["text/html", {"charset", "us-ascii"}]
end
test "content-type with implicit charset" do
message =
parse_email("""
Content-Type: text/html
""")
assert message.headers["content-type"] == ["text/html", {"charset", "us-ascii"}]
end
test "parses encoded word cotaining 'special' characters RFC 2047§6.2" do
message =
parse_email("""
From: =?UTF-8?B?am9obi5kb2VAcmVkYWN0ZS4uLg==?= <[email protected]>
""")
assert message.headers["from"] == {"john.doe@redacte...", "[email protected]"}
end
test "correct handling of encoded words according to RFC 2047 (examples)" do
message =
parse_email("""
From: =?US-ASCII?Q?Keith_Moore?= <[email protected]>
To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <[email protected]>
CC: =?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>
Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=
""")
assert message.headers["from"] == {"Keith Moore", "[email protected]"}
assert message.headers["to"] == [{"Keld J\xF8rn Simonsen", "[email protected]"}]
assert message.headers["cc"] == [{"Andr\xE9 Pirard", "[email protected]"}]
assert message.headers["subject"] == "If you can read this you understand the example."
message =
parse_email("""
From: =?ISO-8859-1?Q?Olle_J=E4rnefors?= <[email protected]>
Subject: Time for ISO 10646?
""")
assert message.headers["from"] == {"Olle J\xE4rnefors", "[email protected]"}
assert message.headers["to"] == ["[email protected]", "[email protected]"]
assert message.headers["subject"] == "Time for ISO 10646?"
message =
parse_email("""
To: Dave Crocker <[email protected]>
From: =?ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?= <[email protected]>
Subject: Re: RFC-HDR care and feeding
""")
assert message.headers["from"] == {"Patrik F\xE4ltstr\xF6m", "[email protected]"}
assert message.headers["to"] == [{"Dave Crocker", "[email protected]"}]
assert message.headers["cc"] == ["[email protected]", "[email protected]"]
assert message.headers["subject"] == "Re: RFC-HDR care and feeding"
message =
parse_email("""
From: Nathaniel Borenstein <[email protected]>
(=?iso-8859-8?b?7eXs+SDv4SDp7Oj08A==?=)
To: Greg Vaudreuil <[email protected]>, Ned Freed
<[email protected]>, Keith Moore <[email protected]>
Subject: Test of new header generator
MIME-Version: 1.0
Content-type: text/plain; charset=ISO-8859-1
""")