Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SendGrid's asm_group_id #368

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/bamboo/adapters/send_grid_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ defmodule Bamboo.SendGridAdapter do
|> put_template_id(email)
|> put_attachments(email)
|> put_categories(email)
|> put_asm_group_id(email)
|> put_settings(config)
end

Expand Down Expand Up @@ -215,6 +216,12 @@ defmodule Bamboo.SendGridAdapter do

defp put_categories(body, _), do: body

defp put_asm_group_id(body, %Email{private: %{asm_group_id: asm_group_id}}) when is_integer(asm_group_id) do
body
|> Map.put(:asm, %{group_id: asm_group_id})
end
defp put_asm_group_id(body, _), do: body

defp put_attachments(body, %Email{attachments: []}), do: body

defp put_attachments(body, %Email{attachments: attachments}) do
Expand Down
18 changes: 18 additions & 0 deletions lib/bamboo/adapters/send_grid_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Bamboo.SendGridHelper do
@id_size 36
@field_name :send_grid_template
@categories :categories
@asm_group_id :asm_group_id

@doc """
Specify the template for SendGrid to use for the context of the substitution
Expand Down Expand Up @@ -83,6 +84,23 @@ defmodule Bamboo.SendGridHelper do
raise "expected a list of category strings"
end

@doc """
An integer id for an ASM (Advanced Suppression Manager) group that this email should belong to.
This can be used to let receipients unsubscribe from only a certain type of communication.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"receipients" -> "recipients"


## Example

email
|> with_asm_group_id(1234)
"""
def with_asm_group_id(email, asm_group_id) when is_integer(asm_group_id) do
email
|> Email.put_private(@asm_group_id, asm_group_id)
end
def with_asm_group_id(_email, asm_group_id) do
raise "expected the asm_group_id parameter to be an integer, got #{asm_group_id}"
end

defp set_template(template, template_id) do
template
|> Map.merge(%{template_id: template_id})
Expand Down
14 changes: 14 additions & 0 deletions test/lib/bamboo/adapters/send_grid_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ defmodule Bamboo.SendGridAdapterTest do
assert personalization["substitutions"] == %{"%foo%" => "bar"}
end

test "deliver/2 correctly handles an asm_group_id" do
email = new_email(
from: {"From", "from@foo.com"},
subject: "My Subject",
)

email
|> Bamboo.SendGridHelper.with_asm_group_id(1234)
|> SendGridAdapter.deliver(@config)

assert_receive {:fake_sendgrid, %{params: params}}
assert params["asm"]["group_id"] == 1234
end

test "deliver/2 doesn't force a subject" do
email = new_email(from: {"From", "from@foo.com"})

Expand Down
11 changes: 11 additions & 0 deletions test/lib/bamboo/adapters/sendgrid_helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ defmodule Bamboo.SendGridHelperTest do

assert length(email.private[:categories]) == 10
end

test "with_asm_group_id/2 adds the correct property", %{email: email} do
email = email |> with_asm_group_id(1234)
assert email.private[:asm_group_id] == 1234
end

test "with_asm_group_id/2 raises on non-integer id", %{email: email} do
assert_raise RuntimeError, fn ->
email |> with_asm_group_id("1234")
end
end
end