diff --git a/.tool-versions b/.tool-versions index 1c2240a0..55abd63c 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -erlang 21.2 -elixir 1.8.1 +erlang 22.3.4 +elixir 1.8 diff --git a/lib/bamboo/adapters/mandrill_adapter.ex b/lib/bamboo/adapters/mandrill_adapter.ex index 38413cb7..fc17cadc 100644 --- a/lib/bamboo/adapters/mandrill_adapter.ex +++ b/lib/bamboo/adapters/mandrill_adapter.ex @@ -103,12 +103,23 @@ defmodule Bamboo.MandrillAdapter do subject: email.subject, text: email.text_body, html: email.html_body, - headers: email.headers, - attachments: attachments(email) + headers: email.headers } + |> add_attachments(email) |> add_message_params(email) end + defp add_attachments(mandrill_message, %{attachments: attachments}) do + {images, files} = + attachments + |> Enum.reverse() + |> Enum.split_with(&is_inline_image?/1) + + mandrill_message + |> Map.put(:attachments, format_attachments(files)) + |> Map.put(:images, format_attachments(images)) + end + defp add_message_params(mandrill_message, %{private: %{message_params: message_params}}) do Enum.reduce(message_params, mandrill_message, fn {key, value}, mandrill_message -> Map.put(mandrill_message, key, value) @@ -117,18 +128,23 @@ defmodule Bamboo.MandrillAdapter do defp add_message_params(mandrill_message, _), do: mandrill_message - defp attachments(%{attachments: attachments}) do - attachments - |> Enum.reverse() - |> Enum.map(fn attachment -> + defp format_attachments(attachments) do + Enum.map(attachments, fn attachment -> + name = if is_inline_image?(attachment), do: attachment.content_id, else: attachment.filename + %{ - name: attachment.filename, + name: name, type: attachment.content_type, content: Base.encode64(attachment.data) } end) end + defp is_inline_image?(%_{content_type: "image/" <> _, content_id: cid}) when not is_nil(cid), + do: true + + defp is_inline_image?(_), do: false + defp recipients(email) do [] |> add_recipients(email.to, type: "to") diff --git a/test/lib/bamboo/adapters/mandrill_adapter_test.exs b/test/lib/bamboo/adapters/mandrill_adapter_test.exs index 1f9abda3..7f0306fb 100644 --- a/test/lib/bamboo/adapters/mandrill_adapter_test.exs +++ b/test/lib/bamboo/adapters/mandrill_adapter_test.exs @@ -1,5 +1,6 @@ defmodule Bamboo.MandrillAdapterTest do use ExUnit.Case + alias Bamboo.Attachment alias Bamboo.Email alias Bamboo.MandrillHelper alias Bamboo.MandrillAdapter @@ -103,6 +104,8 @@ defmodule Bamboo.MandrillAdapterTest do end test "deliver/2 sends from, html and text body, subject, headers and attachment" do + file_path = Path.join(__DIR__, "../../../support/attachment.txt") + email = new_email( from: {"From", "from@foo.com"}, @@ -111,7 +114,20 @@ defmodule Bamboo.MandrillAdapterTest do html_body: "HTML BODY" ) |> Email.put_header("Reply-To", "reply@foo.com") - |> Email.put_attachment(Path.join(__DIR__, "../../../support/attachment.txt")) + |> Email.put_attachment(file_path) + |> Email.put_attachment( + Attachment.new(file_path, content_id: "my_fake_image", filename: "fake_image.jpg") + ) + |> Email.put_attachment(%Attachment{ + content_type: "image/png", + content_id: "my_image", + filename: "my_image.png", + data: + <<137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, + 8, 6, 0, 0, 0, 31, 21, 196, 137, 0, 0, 0, 13, 73, 68, 65, 84, 120, 218, 99, 252, 207, + 192, 80, 15, 0, 4, 133, 1, 128, 132, 169, 140, 33, 0, 0, 0, 0, 73, 69, 78, 68, 174, + 66, 96, 130>> + }) email |> MandrillAdapter.deliver(@config) @@ -131,6 +147,20 @@ defmodule Bamboo.MandrillAdapterTest do "type" => "text/plain", "name" => "attachment.txt", "content" => "VGVzdCBBdHRhY2htZW50Cg==" + }, + %{ + "type" => "text/plain", + "name" => "fake_image.jpg", + "content" => "VGVzdCBBdHRhY2htZW50Cg==" + } + ] + + assert message["images"] == [ + %{ + "type" => "image/png", + "name" => "my_image", + "content" => + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==" } ] end