diff --git a/lib/bamboo/postmark_adapter.ex b/lib/bamboo/postmark_adapter.ex index 4088f6e..e56f3c9 100644 --- a/lib/bamboo/postmark_adapter.ex +++ b/lib/bamboo/postmark_adapter.ex @@ -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 @@ -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: diff --git a/test/lib/bamboo/postmark_adapter_test.exs b/test/lib/bamboo/postmark_adapter_test.exs index e02041d..22e6a7d 100644 --- a/test/lib/bamboo/postmark_adapter_test.exs +++ b/test/lib/bamboo/postmark_adapter_test.exs @@ -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") diff --git a/test/support/attachment.txt b/test/support/attachment.txt new file mode 100644 index 0000000..9a99f35 --- /dev/null +++ b/test/support/attachment.txt @@ -0,0 +1 @@ +Test Attachment \ No newline at end of file