Skip to content

Commit 2877cb1

Browse files
romsahelandrewtimberlake
authored andcommitted
Handle multipart content-type with no parts
Fix parsing of emails that have multipart content-type headers but no actual parts in the body. Previously these would fail to parse correctly. Now they are treated as regular single-part messages with the body content preserved.
1 parent 19d7ac6 commit 2877cb1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/mail/parsers/rfc_2822.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,10 @@ defmodule Mail.Parsers.RFC2822 do
665665
parse(part, opts)
666666
end)
667667

668-
Map.put(message, :parts, parts)
668+
case parts do
669+
[] -> parse_body(Map.put(message, :multipart, false), lines, opts)
670+
_ -> Map.put(message, :parts, parts)
671+
end
669672
end
670673

671674
defp parse_body(%Mail.Message{} = message, [], _opts) do

test/mail/parsers/rfc_2822_test.exs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,23 @@ defmodule Mail.Parsers.RFC2822Test do
967967
""")
968968

969969
assert message.parts == []
970-
assert message.body == nil
970+
assert message.body == ""
971+
end
972+
973+
test "multipart content-type with no parts" do
974+
message =
975+
parse_email("""
976+
To: user@example.com
977+
From: me@example.com
978+
Subject: Test
979+
Content-Type: multipart/mixed; boundary="foobar"
980+
981+
This is a message with multipart content-type but no actual parts
982+
""")
983+
984+
assert message.headers["content-type"] == ["multipart/mixed", {"boundary", "foobar"}]
985+
assert message.body == "This is a message with multipart content-type but no actual parts"
986+
assert message.parts == []
971987
end
972988

973989
test "content-type with explicit charset" do

0 commit comments

Comments
 (0)