Skip to content

Commit

Permalink
Convert attachments from email to Postmark params (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
sax authored May 21, 2020
1 parent 99dfa0f commit 6fd68ff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/bamboo/postmark_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ defmodule Bamboo.PostmarkAdapter do
Map.merge(config, %{api_key: get_key(config)})
end

@doc false
def supports_attachments?, do: true

defp get_key(config) do
api_key =
case Map.get(config, :api_key) do
Expand Down Expand Up @@ -102,6 +105,22 @@ defmodule Bamboo.PostmarkAdapter do
|> email_params()
|> maybe_put_template_params(email)
|> maybe_put_tag_params(email)
|> maybe_put_attachments(email)
end

def maybe_put_attachments(params, %{attachments: []}) do
params
end

def maybe_put_attachments(params, %{attachments: attachments}) do
params
|> Map.put(:"Attachments", Enum.map(attachments, fn attachment ->
%{
Name: attachment.filename,
Content: attachment.data |> Base.encode64(),
ContentType: attachment.content_type
}
end))
end

defp maybe_put_template_params(params, %{private:
Expand Down
19 changes: 19 additions & 0 deletions test/lib/bamboo/postmark_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,25 @@ defmodule Bamboo.PostmarkAdapterTest do
}}
end

test "deliver/2 puts attachments" do
email =
new_email()
|> Email.put_attachment(Path.join(__DIR__, "../../support/attachment.txt"))

PostmarkAdapter.deliver(email, @config)

assert_receive {
:fake_postmark,
%{
params: %{
"Attachments" => [
%{"Content" => "VGVzdCBBdHRhY2htZW50", "ContentType" => "text/plain", "Name" => "attachment.txt"}
]
}
}
}
end

test "raises if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

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 6fd68ff

Please sign in to comment.