Skip to content

Commit

Permalink
Support passing custom params to Postmark
Browse files Browse the repository at this point in the history
Postmark's API accepts several parameters that we don't want to directly support to keep the library's API small and clean. This adds support for passing parameters we don't directly support to the request's body.
  • Loading branch information
whodidthis authored and pablo-co committed Mar 7, 2017
1 parent 9413651 commit 9d5f63d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/bamboo/postmark_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,16 @@ defmodule Bamboo.PostmarkAdapter do
"Headers": email_headers(email),
"TrackOpens": true
}
|> add_message_params(email)
end

defp add_message_params(params, %{private: %{message_params: message_params}}) do
Enum.reduce(message_params, params, fn({key, value}, params) ->
Map.put(params, key, value)
end)
end
defp add_message_params(params, _), do: params

defp email_from(email) do
name = email.from |> elem(0)
email = email.from |> elem(1)
Expand Down
16 changes: 16 additions & 0 deletions lib/bamboo/postmark_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@ defmodule Bamboo.PostmarkHelper do
|> Email.put_private(:template_id, template_id)
|> Email.put_private(:template_model, template_model)
end

@doc """
Put extra message parameters that are used by Postmark. You can set things like TrackOpens or TrackLinks.
## Example
email
|> put_params(email, "TrackLinks", "HtmlAndText")
|> put_params(email, "TrackOpens", true)
"""
def put_param(%Email{private: %{message_params: _}} = email, key, value) do
put_in(email.private[:message_params][key], value)
end
def put_param(email, key, value) do
email |> Email.put_private(:message_params, %{}) |> put_param(key, value)
end

end
17 changes: 14 additions & 3 deletions test/lib/bamboo/postmark_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,22 @@ defmodule Bamboo.PostmarkAdapterTest do
end

test "deliver/2 puts tag param" do
email = new_email() |> PostmarkHelper.tag("some_tag")
email = new_email() |> PostmarkHelper.tag("some_tag")
email |> PostmarkAdapter.deliver(@config)
assert_receive {:fake_postmark, %{params: %{"Tag" => "some_tag"}}}
end

email |> PostmarkAdapter.deliver(@config)
test "deliver/2 puts tracking params" do
email = new_email()
|> PostmarkHelper.template("hello")
|> PostmarkHelper.put_param("TrackOpens", true)
|> PostmarkHelper.put_param("TrackLinks", "HtmlOnly")

email |> PostmarkAdapter.deliver(@config)

assert_receive {:fake_postmark, %{params: %{"Tag" => "some_tag"}}}
assert_receive {:fake_postmark, %{params: %{
"TrackLinks" => "HtmlOnly", "TrackOpens" => true, "TemplateId" => "hello"}
}}
end

test "raises if the response is not a success" do
Expand Down

0 comments on commit 9d5f63d

Please sign in to comment.