Skip to content

Commit

Permalink
Merge branch 'master' into issue-beam-community#502
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspork authored Sep 30, 2024
2 parents 778bbc9 + 53bd392 commit 539a432
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 17 deletions.
13 changes: 13 additions & 0 deletions lib/bamboo/adapters/send_grid_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ defmodule Bamboo.SendGridAdapter do
|> put_asm_group_id(email)
|> put_bypass_list_management(email)
|> put_google_analytics(email)
|> put_click_tracking(email)
|> put_ip_pool_name(email)
|> put_unique_args(email)
end
Expand Down Expand Up @@ -355,6 +356,18 @@ defmodule Bamboo.SendGridAdapter do

defp put_google_analytics(body, _), do: body

defp put_click_tracking(body, %Email{private: %{click_tracking_enabled: enabled}}) do
tracking_settings =
body
|> Map.get(:tracking_settings, %{})
|> Map.put(:click_tracking, %{enable: enabled, enable_text: enabled})

body
|> Map.put(:tracking_settings, tracking_settings)
end

defp put_click_tracking(body, _), do: body

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

defp put_attachments(body, %Email{attachments: attachments}) do
Expand Down
22 changes: 22 additions & 0 deletions lib/bamboo/adapters/send_grid_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Bamboo.SendGridHelper do
@send_at_field :sendgrid_send_at
@ip_pool_name_field :ip_pool_name
@unique_args :unique_args
@click_tracking_enabled :click_tracking_enabled

@doc """
Specify the template for SendGrid to use for the context of the substitution
Expand Down Expand Up @@ -200,6 +201,27 @@ defmodule Bamboo.SendGridHelper do
raise "expected with_google_analytics enabled parameter to be a boolean"
end

@doc """
Instruct SendGrid to enable or disable Click Tracking for a particular email.
Read more about SendGrid click tracking [here](https://docs.sendgrid.com/ui/account-and-settings/tracking#click-tracking)
## Example
email
|> with_click_tracking(true)
email
|> with_click_tracking(false)
"""
def with_click_tracking(email, enabled)
when is_boolean(enabled),
do: Email.put_private(email, @click_tracking_enabled, enabled)

def with_click_tracking(_email, _enabled) do
raise "expected with_click_tracking enabled parameter to be a boolean"
end

@doc """
Schedule a time for SendGrid to deliver the email.
Expand Down
45 changes: 29 additions & 16 deletions lib/bamboo/plug/sent_email_viewer/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@
margin: 0 0 12px 0;
font-size: 15px;
font-weight: 700;
color: #2D9EB9;
}
.email-detail-body-label + .email-detail-body-label {
margin-top: 30px;
}
.email-detail-recipients,
Expand Down Expand Up @@ -228,22 +233,30 @@
</section>

<section class="email-detail-bodies-container">
<h3 class="email-detail-body-label">HTML body</h3>
<p class="email-detail-body">
<script>
function adjustFrameHeight(iframe) {
var contentWindow = iframe.contentWindow;
var height = (contentWindow.outerHeight)
? contentWindow.outerHeight
: contentWindow.screen.availHeight;
iframe.style.height = height + "px";
}
</script>
<iframe onload="adjustFrameHeight(this)" src="<%= "#{@base_path}/#{Bamboo.SentEmail.get_id(@selected_email)}/html" %>"></iframe>
</p>

<h3 class="email-detail-body-label">Text Body</h3>
<pre class="email-detail-body"><%= Bamboo.SentEmailViewerPlug.Helper.format_text_body(@selected_email.text_body) %></pre>
<%= if @selected_email.html_body do %>
<h3 class="email-detail-body-label">HTML body</h3>
<p class="email-detail-body">
<script>
function adjustFrameHeight(iframe) {
var contentWindow = iframe.contentWindow;
var height = (contentWindow.outerHeight)
? contentWindow.outerHeight
: contentWindow.screen.availHeight;
iframe.style.height = height + "px";
}
</script>
<iframe onload="adjustFrameHeight(this)" src="<%= "#{@base_path}/#{Bamboo.SentEmail.get_id(@selected_email)}/html" %>"></iframe>
</p>
<% else %>
<h3 class="email-detail-body-label">No HTML body</h3>
<% end %>

<%= if @selected_email.text_body do %>
<h3 class="email-detail-body-label">Text body</h3>
<pre class="email-detail-body"><%= Bamboo.SentEmailViewerPlug.Helper.format_text_body(@selected_email.text_body) %></pre>
<% else %>
<h3 class="email-detail-body-label">No text body</h3>
<% end %>
</section>
</section>
</main>
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule Bamboo.Mixfile do
defp deps do
[
{:plug, "~> 1.0"},
{:mime, "~> 1.4"},
{:mime, "~> 1.4 or ~> 2.0"},
{:ex_machina, "~> 2.4", only: :test},
{:cowboy, "~> 1.0", only: [:test, :dev]},
{:excoveralls, "~> 0.13", only: :test},
Expand Down
32 changes: 32 additions & 0 deletions test/lib/bamboo/adapters/send_grid_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,38 @@ defmodule Bamboo.SendGridAdapterTest do
assert params["tracking_settings"]["ganalytics"]["utm_content"] == "content"
end

test "deliver/2 correctly handles when with_click_tracking is enabled" do
email =
new_email(
from: {"From", "[email protected]"},
subject: "My Subject"
)

email
|> Bamboo.SendGridHelper.with_click_tracking(true)
|> SendGridAdapter.deliver(@config)

assert_receive {:fake_sendgrid, %{params: params}}
assert params["tracking_settings"]["click_tracking"]["enable"] == true
assert params["tracking_settings"]["click_tracking"]["enable_text"] == true
end

test "deliver/2 correctly handles when with_click_tracking is disabled" do
email =
new_email(
from: {"From", "[email protected]"},
subject: "My Subject"
)

email
|> Bamboo.SendGridHelper.with_click_tracking(false)
|> SendGridAdapter.deliver(@config)

assert_receive {:fake_sendgrid, %{params: params}}
assert params["tracking_settings"]["click_tracking"]["enable"] == false
assert params["tracking_settings"]["click_tracking"]["enable_text"] == false
end

test "deliver/2 correctly handles a sendgrid_send_at timestamp" do
email =
new_email(
Expand Down
18 changes: 18 additions & 0 deletions test/lib/bamboo/adapters/send_grid_helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ defmodule Bamboo.SendGridHelperTest do
end
end

test "with_click_tracking/2 with utm_params", %{email: email} do
email = with_click_tracking(email, true)

assert email.private[:click_tracking_enabled] == true
end

test "with_click_tracking/2 with enabled set false", %{email: email} do
email = with_click_tracking(email, false)

assert email.private[:click_tracking_enabled] == false
end

test "with_click_tracking/2 raises on non-boolean enabled parameter", %{email: email} do
assert_raise RuntimeError, fn ->
with_click_tracking(email, 1)
end
end

describe "with_send_at/2" do
test "adds the correct property for a DateTime input", %{email: email} do
{:ok, datetime, _} = DateTime.from_iso8601("2020-01-31T15:46:00Z")
Expand Down

0 comments on commit 539a432

Please sign in to comment.