Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/mail/renderers/rfc_2822.ex
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,12 @@ defmodule Mail.Renderers.RFC2822 do
Mail.Message.put_content_type(message, "multipart/mixed")
else
alternative =
case text_parts do
[part] ->
part

text_parts ->
Mail.build_multipart()
|> Mail.Message.put_content_type("multipart/alternative")
|> Mail.Message.put_parts(text_parts)
if match?([_part], text_parts) && attachments != [] do
List.first(text_parts)
else
Mail.build_multipart()
|> Mail.Message.put_content_type("multipart/alternative")
|> Mail.Message.put_parts(text_parts)
end

related =
Expand Down
24 changes: 24 additions & 0 deletions test/mail/renderers/rfc_2822_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,30 @@ defmodule Mail.Renderers.RFC2822Test do
end

describe "multipart configuration" do
# This test ensures that Mail.build_multipart() is respected even if only one part is supplied
test "multipart/alternative with only one part and no attachments" do
message =
Mail.build_multipart()
|> Mail.put_to("user1@example.com")
|> Mail.put_from({"User2", "user2@example.com"})
|> Mail.put_subject("Test email")
|> Mail.put_text("Some text")
|> Mail.Renderers.RFC2822.render()
|> Mail.Parsers.RFC2822.parse()

assert %Mail.Message{
headers: %{"content-type" => ["multipart/alternative", {"boundary", _boundary}]},
parts: [
%Mail.Message{
headers: %{"content-type" => ["text/plain", {"charset", "UTF-8"}]},
body: "Some text",
parts: [],
multipart: false
}
]
} = message
end

test "multipart/alternative with text/plain and text/html" do
message =
Mail.build_multipart()
Expand Down