From 8b8053fb9153f94b572f252a7dc8b558191e291c Mon Sep 17 00:00:00 2001 From: Clairton Rodrigo Heinzen Date: Fri, 6 Jul 2018 17:01:05 -0300 Subject: [PATCH 1/4] Add custom args to personalization --- lib/bamboo/adapters/send_grid_adapter.ex | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/bamboo/adapters/send_grid_adapter.ex b/lib/bamboo/adapters/send_grid_adapter.ex index fcf98797..4675a494 100644 --- a/lib/bamboo/adapters/send_grid_adapter.ex +++ b/lib/bamboo/adapters/send_grid_adapter.ex @@ -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 @@ -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 @@ -194,6 +195,14 @@ defmodule Bamboo.SendGridAdapter do defp put_template_substitutions(body, _), 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 From 8447b9d1410dda0e749e3614d520a6feb5f57ad1 Mon Sep 17 00:00:00 2001 From: Clairton Rodrigo Heinzen Date: Wed, 11 Jul 2018 11:28:00 -0300 Subject: [PATCH 2/4] if is empty not add custom_args --- lib/bamboo/adapters/send_grid_adapter.ex | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/bamboo/adapters/send_grid_adapter.ex b/lib/bamboo/adapters/send_grid_adapter.ex index 4675a494..5a6ee081 100644 --- a/lib/bamboo/adapters/send_grid_adapter.ex +++ b/lib/bamboo/adapters/send_grid_adapter.ex @@ -195,6 +195,10 @@ defmodule Bamboo.SendGridAdapter do defp put_template_substitutions(body, _), do: body + defp put_custom_args(body, %Email{private: %{custom_args: custom_args}}) + when length(custom_args) <= 0, + do: body + defp put_custom_args(body, %Email{ private: %{custom_args: custom_args} }) do From ccc246092e7f2c748df5458a5516fda4b0e1fce5 Mon Sep 17 00:00:00 2001 From: Clairton Rodrigo Heinzen Date: Wed, 11 Jul 2018 16:27:50 -0300 Subject: [PATCH 3/4] if is nil not add custom_args --- lib/bamboo/adapters/send_grid_adapter.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bamboo/adapters/send_grid_adapter.ex b/lib/bamboo/adapters/send_grid_adapter.ex index 5a6ee081..d41dd3d6 100644 --- a/lib/bamboo/adapters/send_grid_adapter.ex +++ b/lib/bamboo/adapters/send_grid_adapter.ex @@ -196,7 +196,7 @@ defmodule Bamboo.SendGridAdapter do defp put_template_substitutions(body, _), do: body defp put_custom_args(body, %Email{private: %{custom_args: custom_args}}) - when length(custom_args) <= 0, + when is_nil(custom_args) or length(custom_args) == 0, do: body defp put_custom_args(body, %Email{ From f2e4ddf177bbcf2c90d010c53c2fa9251fdf1b01 Mon Sep 17 00:00:00 2001 From: Clairton Rodrigo Heinzen Date: Tue, 7 Aug 2018 19:13:09 -0300 Subject: [PATCH 4/4] test custom_args --- .../adapters/send_grid_adapter_test.exs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/lib/bamboo/adapters/send_grid_adapter_test.exs b/test/lib/bamboo/adapters/send_grid_adapter_test.exs index 4045711a..d332a165 100644 --- a/test/lib/bamboo/adapters/send_grid_adapter_test.exs +++ b/test/lib/bamboo/adapters/send_grid_adapter_test.exs @@ -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(