Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused multipart/mixed when there's no attachments #199

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 21 additions & 6 deletions lib/bamboo/adapters/smtp_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,24 @@ defmodule Bamboo.SMTPAdapter do
|> add_smtp_line("X-Attachment-Id: #{random}")
end

defp add_multipart_attachment_headers(
body,
%Bamboo.Email{attachments: []},
_multi_part_mixed_delimiter
),
do: body

defp add_multipart_attachment_headers(
body,
%Bamboo.Email{attachments: _attachments},
multi_part_mixed_delimiter
) do
body
|> add_multipart_mixed_header(multi_part_mixed_delimiter)
|> add_ending_header
|> add_multipart_delimiter(multi_part_mixed_delimiter)
end

defp add_attachment_body(body, data) do
data =
if String.contains?(body, "Content-Type: message/rfc822") do
Expand All @@ -297,14 +315,14 @@ defmodule Bamboo.SMTPAdapter do
|> add_attachment_body(attachment.data)
end

defp add_attachments(body, %Bamboo.Email{attachments: nil}, _), do: body
defp add_attachments(body, %Bamboo.Email{attachments: []}, _), do: body

defp add_attachments(body, %Bamboo.Email{attachments: attachments}, multi_part_mixed_delimiter) do
attachment_part =
attachments
|> Enum.map(fn attachment -> add_attachment(attachment, multi_part_mixed_delimiter) end)

"#{body}#{attachment_part}"
add_ending_multipart("#{body}#{attachment_part}", multi_part_mixed_delimiter)
end

defp add_to(body, %Bamboo.Email{to: recipients}) do
Expand Down Expand Up @@ -342,16 +360,13 @@ defmodule Bamboo.SMTPAdapter do
|> add_to(email)
|> add_custom_headers(email)
|> add_mime_header
|> add_multipart_mixed_header(multi_part_mixed_delimiter)
|> add_ending_header
|> add_multipart_delimiter(multi_part_mixed_delimiter)
|> add_multipart_attachment_headers(email, multi_part_mixed_delimiter)
|> add_multipart_header(multi_part_delimiter)
|> add_ending_header
|> add_text_body(email, multi_part_delimiter)
|> add_html_body(email, multi_part_delimiter)
|> add_ending_multipart(multi_part_delimiter)
|> add_attachments(email, multi_part_mixed_delimiter)
|> add_ending_multipart(multi_part_mixed_delimiter)
end

defp build_error({:ok, value}, _key, errors) when value != nil, do: errors
Expand Down
33 changes: 32 additions & 1 deletion test/lib/bamboo/adapters/smtp_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ defmodule Bamboo.SMTPAdapterTest do
SMTPAdapter.deliver(bamboo_email, bamboo_config)
end

test "emails looks fine when only text body is set" do
test "emails looks fine when only html body is set" do
bamboo_email = new_email(text_body: nil)
bamboo_config = configuration()

Expand Down Expand Up @@ -579,6 +579,37 @@ defmodule Bamboo.SMTPAdapterTest do
assert_configuration(bamboo_config, gen_smtp_config)
end

test "emails contain multipart/mixed only when sent with attachments" do
bamboo_email = new_email()
bamboo_config = configuration()

{:ok, "200 Ok 1234567890"} = SMTPAdapter.deliver(bamboo_email, bamboo_config)

bamboo_email_with_attachments =
new_email()
|> Bamboo.Email.put_attachment(Path.absname("test/attachments/attachment_two.txt"),
filename: "some_attachment.txt"
)

{:ok, "200 Ok 1234567890"} = SMTPAdapter.deliver(bamboo_email_with_attachments, bamboo_config)

assert 2 = length(FakeGenSMTP.fetch_sent_emails())

[{{_, _, raw_email_2}, _}, {{_, _, raw_email}, _}] = FakeGenSMTP.fetch_sent_emails()

assert String.contains?(raw_email, "MIME-Version: 1.0\r\n")

refute String.contains?(
raw_email,
"multipart/mixed"
)

assert String.contains?(
raw_email_2,
"multipart/mixed"
)
end

test "email looks fine when no bcc: is set" do
bamboo_email = new_email(bcc: [])
bamboo_config = configuration()
Expand Down