Skip to content

Commit

Permalink
Add inline images in mandrill adapter (#608)
Browse files Browse the repository at this point in the history
This commit adds support to send to mandrill images inline.
  • Loading branch information
agutierrezrodriguez authored Jun 25, 2021
1 parent 2eb0124 commit b82c988
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
erlang 21.2
elixir 1.8.1
erlang 22.3.4
elixir 1.8
30 changes: 23 additions & 7 deletions lib/bamboo/adapters/mandrill_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
32 changes: 31 additions & 1 deletion test/lib/bamboo/adapters/mandrill_adapter_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Bamboo.MandrillAdapterTest do
use ExUnit.Case
alias Bamboo.Attachment
alias Bamboo.Email
alias Bamboo.MandrillHelper
alias Bamboo.MandrillAdapter
Expand Down Expand Up @@ -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", "[email protected]"},
Expand All @@ -111,7 +114,20 @@ defmodule Bamboo.MandrillAdapterTest do
html_body: "HTML BODY"
)
|> Email.put_header("Reply-To", "[email protected]")
|> 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)

Expand All @@ -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
Expand Down

0 comments on commit b82c988

Please sign in to comment.