diff --git a/lib/bamboo/adapters/smtp_adapter.ex b/lib/bamboo/adapters/smtp_adapter.ex index 62d1c05..1cc3e0b 100644 --- a/lib/bamboo/adapters/smtp_adapter.ex +++ b/lib/bamboo/adapters/smtp_adapter.ex @@ -225,7 +225,19 @@ defmodule Bamboo.SMTPAdapter do |> add_smtp_line(text_body) end - defp add_attachment_header(body, %{content_type: content_type} = attachment) + defp add_attachment_header(body, attachment) do + case attachment.content_id do + nil -> + add_common_attachment_header(body, attachment) + + cid -> + body + |> add_common_attachment_header(attachment) + |> add_smtp_line("Content-ID: <#{cid}>") + end + end + + defp add_common_attachment_header(body, %{content_type: content_type} = attachment) when content_type == "message/rfc822" do <> = :crypto.strong_rand_bytes(4) @@ -235,7 +247,7 @@ defmodule Bamboo.SMTPAdapter do |> add_smtp_line("X-Attachment-Id: #{random}") end - defp add_attachment_header(body, attachment) do + defp add_common_attachment_header(body, attachment) do <> = :crypto.strong_rand_bytes(4) body diff --git a/test/attachments/attachment_one.txt b/test/attachments/attachment_one.txt new file mode 100644 index 0000000..3e2caaf --- /dev/null +++ b/test/attachments/attachment_one.txt @@ -0,0 +1 @@ +Attachment One. diff --git a/test/attachments/attachment_two.txt b/test/attachments/attachment_two.txt new file mode 100644 index 0000000..e4888d3 --- /dev/null +++ b/test/attachments/attachment_two.txt @@ -0,0 +1 @@ +Attachment Two. diff --git a/test/lib/bamboo/adapters/smtp_adapter_test.exs b/test/lib/bamboo/adapters/smtp_adapter_test.exs index be72542..08b9ac9 100644 --- a/test/lib/bamboo/adapters/smtp_adapter_test.exs +++ b/test/lib/bamboo/adapters/smtp_adapter_test.exs @@ -730,6 +730,28 @@ defmodule Bamboo.SMTPAdapterTest do assert_configuration(bamboo_config, gen_smtp_config) end + test "email have a Content-ID properly set when attaching files with content_id" do + bamboo_email = + new_email() + |> Bamboo.Email.put_attachment(Path.absname("test/attachments/attachment_one.txt"), + content_id: "12345" + ) + |> Bamboo.Email.put_attachment(Path.absname("test/attachments/attachment_two.txt"), + content_id: "54321" + ) + + bamboo_config = configuration() + + {:ok, "200 Ok 1234567890"} = SMTPAdapter.deliver(bamboo_email, bamboo_config) + + assert 1 = length(FakeGenSMTP.fetch_sent_emails()) + + [{{_from, _to, raw_email}, _gen_smtp_config}] = FakeGenSMTP.fetch_sent_emails() + + assert Regex.run(~r{Content-ID: <12345>\r\n}, raw_email, capture: :all_but_first) + assert Regex.run(~r{Content-ID: <54321>\r\n}, raw_email, capture: :all_but_first) + end + test "check rfc822 encoding for subject" do bamboo_email = @email_in_utf8