Skip to content

Commit

Permalink
Add attachment to Mandrill Adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
hisea authored and paulcsmith committed May 5, 2017
1 parent 8dff421 commit d478331
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/bamboo/adapters/mandrill_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ defmodule Bamboo.MandrillAdapter do
subject: email.subject,
text: email.text_body,
html: email.html_body,
headers: email.headers
headers: email.headers,
attachments: attachments(email)
}
|> add_message_params(email)
end
Expand All @@ -101,6 +102,18 @@ defmodule Bamboo.MandrillAdapter do
end
defp add_message_params(mandrill_message, _), do: mandrill_message

defp attachments(%{attachments: attachments}) do
attachments
|> Enum.reverse
|> Enum.map(fn(att) ->
%{
name: att.filename,
type: att.content_type,
content: Base.encode64(File.read!(att.path))
}
end)
end

defp recipients(email) do
[]
|> add_recipients(email.to, type: "to")
Expand Down
10 changes: 9 additions & 1 deletion test/lib/bamboo/adapters/mandrill_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ defmodule Bamboo.MandrillAdapterTest do
assert request_path == "/api/1.0/messages/send-template.json"
end

test "deliver/2 sends from, html and text body, subject, and headers" do
test "deliver/2 sends from, html and text body, subject, headers and attachment" do
email = new_email(
from: {"From", "from@foo.com"},
subject: "My Subject",
text_body: "TEXT BODY",
html_body: "HTML BODY",
)
|> Email.put_header("Reply-To", "reply@foo.com")
|> Email.put_attachment(Path.join(__DIR__, "../../../support/attachment.txt"))

email |> MandrillAdapter.deliver(@config)

Expand All @@ -113,6 +114,13 @@ defmodule Bamboo.MandrillAdapterTest do
assert message["text"] == email.text_body
assert message["html"] == email.html_body
assert message["headers"] == email.headers
assert message["attachments"] == [
%{
"type" => "text/plain",
"name" => "attachment.txt",
"content" => "VGVzdCBBdHRhY2htZW50Cg=="
}
]
end

test "deliver/2 correctly formats recipients" do
Expand Down
1 change: 1 addition & 0 deletions test/support/attachment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test Attachment

0 comments on commit d478331

Please sign in to comment.