Skip to content

Commit

Permalink
Possible send custom args in SendGridAdapter (#413)
Browse files Browse the repository at this point in the history
* Add custom args to personalization

* if is empty not add custom_args

* if is nil not add custom_args

* test custom_args
  • Loading branch information
clairton authored and paulcsmith committed Aug 15, 2018
1 parent 858f305 commit 907d761
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/bamboo/adapters/send_grid_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Bamboo.SendGridAdapter do
config :my_app, MyApp.Mailer,
adapter: Bamboo.SendGridAdapter,
api_key: "my_api_key" # or {:system, "SENDGRID_API_KEY"}
# To enable sandbox mode (e.g. in development or staging environments),
# in config/dev.exs or config/prod.exs etc
config :my_app, MyApp.Mailer, sandbox: true
Expand Down Expand Up @@ -119,6 +119,7 @@ defmodule Bamboo.SendGridAdapter do
|> put_to(email)
|> put_cc(email)
|> put_bcc(email)
|> put_custom_args(email)
|> put_template_substitutions(email)
end

Expand Down Expand Up @@ -194,6 +195,18 @@ defmodule Bamboo.SendGridAdapter do

defp put_template_substitutions(body, _), do: body

defp put_custom_args(body, %Email{private: %{custom_args: custom_args}})
when is_nil(custom_args) or length(custom_args) == 0,
do: body

defp put_custom_args(body, %Email{
private: %{custom_args: custom_args}
}) do
Map.put(body, :custom_args, custom_args)
end

defp put_custom_args(body, _), do: body

defp put_categories(body, %Email{private: %{categories: categories}})
when is_list(categories) and length(categories) <= 10 do
body
Expand Down
23 changes: 23 additions & 0 deletions test/lib/bamboo/adapters/send_grid_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ defmodule Bamboo.SendGridAdapterTest do
]
end

test "deliver/2 correctly custom args" do
email = new_email()

email
|> Email.put_private(:custom_args, %{post_code: "123"})
|> SendGridAdapter.deliver(@config)

assert_receive {:fake_sendgrid, %{params: params}}
personalization = List.first(params["personalizations"])
assert personalization["custom_args"] == %{"post_code" => "123"}
end

test "deliver/2 without custom args" do
email = new_email()

email
|> SendGridAdapter.deliver(@config)

assert_receive {:fake_sendgrid, %{params: params}}
personalization = List.first(params["personalizations"])
assert personalization["custom_args"] == nil
end

test "deliver/2 correctly formats recipients" do
email =
new_email(
Expand Down

0 comments on commit 907d761

Please sign in to comment.