From cc60f1c94209cd61f5b5b944cd1015ff9c6dc709 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Mon, 21 Aug 2023 19:21:20 +0300 Subject: [PATCH] Add new priority email template (#3283) * Add new priority email template? * Allow priority email to be sent with not layout * Use priority email template for priority transactional emails * Test for postmark message stream --- lib/plausible_web/email.ex | 65 ++++++++------- .../templates/layout/base_email.html.eex | 2 - .../templates/layout/priority_email.html.eex | 13 +++ test/plausible_web/email_test.exs | 83 +++++++++++++++---- 4 files changed, 118 insertions(+), 45 deletions(-) create mode 100644 lib/plausible_web/templates/layout/priority_email.html.eex diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index 0737cfea5..17df60ae6 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -7,7 +7,7 @@ defmodule PlausibleWeb.Email do end def activation_email(user, code) do - base_email() + priority_email() |> to(user) |> tag("activation-email") |> subject("#{code} is your Plausible email verification code") @@ -19,7 +19,7 @@ defmodule PlausibleWeb.Email do |> to(user) |> tag("welcome-email") |> subject("Welcome to Plausible") - |> render("welcome_email.html", user: user, unsubscribe: true) + |> render("welcome_email.html", user: user) end def create_site_email(user) do @@ -27,7 +27,7 @@ defmodule PlausibleWeb.Email do |> to(user) |> tag("create-site-email") |> subject("Your Plausible setup: Add your website details") - |> render("create_site_email.html", user: user, unsubscribe: true) + |> render("create_site_email.html", user: user) end def site_setup_help(user, site) do @@ -37,8 +37,7 @@ defmodule PlausibleWeb.Email do |> subject("Your Plausible setup: Waiting for the first page views") |> render("site_setup_help_email.html", user: user, - site: site, - unsubscribe: true + site: site ) end @@ -49,8 +48,7 @@ defmodule PlausibleWeb.Email do |> subject("Plausible is now tracking your website stats") |> render("site_setup_success_email.html", user: user, - site: site, - unsubscribe: true + site: site ) end @@ -59,11 +57,11 @@ defmodule PlausibleWeb.Email do |> to(user) |> tag("check-stats-email") |> subject("Check your Plausible website stats") - |> render("check_stats_email.html", user: user, unsubscribe: true) + |> render("check_stats_email.html", user: user) end def password_reset_email(email, reset_link) do - base_email(%{layout: nil}) + priority_email(%{layout: nil}) |> to(email) |> tag("password-reset-email") |> subject("Plausible password reset") @@ -75,7 +73,7 @@ defmodule PlausibleWeb.Email do |> to(user) |> tag("trial-one-week-reminder") |> subject("Your Plausible trial expires next week") - |> render("trial_one_week_reminder.html", user: user, unsubscribe: true) + |> render("trial_one_week_reminder.html", user: user) end def trial_upgrade_email(user, day, {pageviews, custom_events}) do @@ -90,8 +88,7 @@ defmodule PlausibleWeb.Email do day: day, custom_events: custom_events, usage: pageviews + custom_events, - suggested_plan: suggested_plan, - unsubscribe: true + suggested_plan: suggested_plan ) end @@ -100,7 +97,7 @@ defmodule PlausibleWeb.Email do |> to(user) |> tag("trial-over-email") |> subject("Your Plausible trial has ended") - |> render("trial_over_email.html", user: user, unsubscribe: true) + |> render("trial_over_email.html", user: user) end def weekly_report(email, site, assigns) do @@ -127,7 +124,7 @@ defmodule PlausibleWeb.Email do end def over_limit_email(user, usage, last_cycle, suggested_plan) do - base_email() + priority_email() |> to(user) |> tag("over-limit") |> subject("[Action required] You have outgrown your Plausible subscription tier") @@ -135,8 +132,7 @@ defmodule PlausibleWeb.Email do user: user, usage: usage, last_cycle: last_cycle, - suggested_plan: suggested_plan, - unsubscribe: true + suggested_plan: suggested_plan }) end @@ -155,7 +151,7 @@ defmodule PlausibleWeb.Email do end def dashboard_locked(user, usage, last_cycle, suggested_plan) do - base_email() + priority_email() |> to(user) |> tag("dashboard-locked") |> subject("[Action required] Your Plausible dashboard is now locked") @@ -170,7 +166,7 @@ defmodule PlausibleWeb.Email do def yearly_renewal_notification(user) do date = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}") - base_email() + priority_email() |> to(user) |> tag("yearly-renewal") |> subject("Your Plausible subscription is up for renewal") @@ -185,7 +181,7 @@ defmodule PlausibleWeb.Email do def yearly_expiration_notification(user) do date = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}") - base_email() + priority_email() |> to(user) |> tag("yearly-expiration") |> subject("Your Plausible subscription is about to expire") @@ -204,7 +200,7 @@ defmodule PlausibleWeb.Email do end def new_user_invitation(invitation) do - base_email() + priority_email() |> to(invitation.email) |> tag("new-user-invitation") |> subject("[Plausible Analytics] You've been invited to #{invitation.site.domain}") @@ -214,7 +210,7 @@ defmodule PlausibleWeb.Email do end def existing_user_invitation(invitation) do - base_email() + priority_email() |> to(invitation.email) |> tag("existing-user-invitation") |> subject("[Plausible Analytics] You've been invited to #{invitation.site.domain}") @@ -224,7 +220,7 @@ defmodule PlausibleWeb.Email do end def ownership_transfer_request(invitation, new_owner_account) do - base_email() + priority_email() |> to(invitation.email) |> tag("ownership-transfer-request") |> subject("[Plausible Analytics] Request to transfer ownership of #{invitation.site.domain}") @@ -235,7 +231,7 @@ defmodule PlausibleWeb.Email do end def invitation_accepted(invitation) do - base_email() + priority_email() |> to(invitation.inviter.email) |> tag("invitation-accepted") |> subject( @@ -248,7 +244,7 @@ defmodule PlausibleWeb.Email do end def invitation_rejected(invitation) do - base_email() + priority_email() |> to(invitation.inviter.email) |> tag("invitation-rejected") |> subject( @@ -261,7 +257,7 @@ defmodule PlausibleWeb.Email do end def ownership_transfer_accepted(invitation) do - base_email() + priority_email() |> to(invitation.inviter.email) |> tag("ownership-transfer-accepted") |> subject( @@ -274,7 +270,7 @@ defmodule PlausibleWeb.Email do end def ownership_transfer_rejected(invitation) do - base_email() + priority_email() |> to(invitation.inviter.email) |> tag("ownership-transfer-rejected") |> subject( @@ -287,7 +283,7 @@ defmodule PlausibleWeb.Email do end def site_member_removed(membership) do - base_email() + priority_email() |> to(membership.user.email) |> tag("site-member-removed") |> subject("[Plausible Analytics] Your access to #{membership.site.domain} has been revoked") @@ -298,7 +294,7 @@ defmodule PlausibleWeb.Email do end def import_success(user, site) do - base_email() + priority_email() |> to(user) |> tag("import-success-email") |> subject("Google Analytics data imported for #{site.domain}") @@ -311,7 +307,7 @@ defmodule PlausibleWeb.Email do end def import_failure(user, site) do - base_email() + priority_email() |> to(user) |> tag("import-failure-email") |> subject("Google Analytics import failed for #{site.domain}") @@ -337,6 +333,17 @@ defmodule PlausibleWeb.Email do }) end + @doc """ + Unlike the default 'base' emails, priority emails cannot be unsubscribed from. This is achieved + by sending them through a dedicated 'priority' message stream in Postmark. + """ + def priority_email(), do: priority_email(%{layout: "priority_email.html"}) + + def priority_email(%{layout: layout}) do + base_email(%{layout: layout}) + |> put_param("MessageStream", "priority") + end + def base_email(), do: base_email(%{layout: "base_email.html"}) def base_email(%{layout: layout}) do diff --git a/lib/plausible_web/templates/layout/base_email.html.eex b/lib/plausible_web/templates/layout/base_email.html.eex index 5fd940ad9..0679b338b 100644 --- a/lib/plausible_web/templates/layout/base_email.html.eex +++ b/lib/plausible_web/templates/layout/base_email.html.eex @@ -12,6 +12,4 @@ The Plausible Team 💌

<%= link(plausible_url(), to: plausible_url()) %>
-<%= if Map.get(assigns, :unsubscribe, false) do %> {{{ pm:unsubscribe }}} -<% end %> diff --git a/lib/plausible_web/templates/layout/priority_email.html.eex b/lib/plausible_web/templates/layout/priority_email.html.eex new file mode 100644 index 000000000..bddd75b9d --- /dev/null +++ b/lib/plausible_web/templates/layout/priority_email.html.eex @@ -0,0 +1,13 @@ +<%= PlausibleWeb.EmailView.greet_recipient(assigns) %> +

+ +<%= @inner_content %> +

+ +Regards,
+The Plausible Team 💌 +

+ +-- +

+<%= link(plausible_url(), to: plausible_url()) %>
diff --git a/test/plausible_web/email_test.exs b/test/plausible_web/email_test.exs index 578d8c386..ed510d132 100644 --- a/test/plausible_web/email_test.exs +++ b/test/plausible_web/email_test.exs @@ -7,7 +7,7 @@ defmodule PlausibleWeb.EmailTest do test "greets user by first name if user in template assigns" do email = Email.base_email() - |> Email.render("activation_email.html", %{ + |> Email.render("welcome_email.html", %{ user: build(:user, name: "John Doe"), code: "123" }) @@ -18,9 +18,7 @@ defmodule PlausibleWeb.EmailTest do test "greets impersonally when user not in template assigns" do email = Email.base_email() - |> Email.render("password_reset_email.html", %{ - reset_link: "imaginary" - }) + |> Email.render("welcome_email.html") assert email.html_body =~ "Hey," end @@ -28,24 +26,16 @@ defmodule PlausibleWeb.EmailTest do test "renders plausible link" do email = Email.base_email() - |> Email.render("cancellation_email.html") + |> Email.render("welcome_email.html") assert email.html_body =~ plausible_link() end - test "does not render unsubscribe placeholder by default" do + test "renders unsubscribe placeholder" do email = Email.base_email() |> Email.render("welcome_email.html") - refute email.html_body =~ "{{{ pm:unsubscribe }}}" - end - - test "renders unsubscribe placeholder if told" do - email = - Email.base_email() - |> Email.render("welcome_email.html", %{unsubscribe: true}) - assert email.html_body =~ "{{{ pm:unsubscribe }}}" end @@ -61,6 +51,71 @@ defmodule PlausibleWeb.EmailTest do end end + describe "priority email layout" do + test "uses the `priority` message stream in Postmark" do + email = + Email.priority_email() + |> Email.render("activation_email.html", %{ + user: build(:user, name: "John Doe"), + code: "123" + }) + + assert %{"MessageStream" => "priority"} = email.private[:message_params] + end + + test "greets user by first name if user in template assigns" do + email = + Email.priority_email() + |> Email.render("activation_email.html", %{ + user: build(:user, name: "John Doe"), + code: "123" + }) + + assert email.html_body =~ "Hey John," + end + + test "greets impersonally when user not in template assigns" do + email = + Email.priority_email() + |> Email.render("password_reset_email.html", %{ + reset_link: "imaginary" + }) + + assert email.html_body =~ "Hey," + end + + test "renders plausible link" do + email = + Email.priority_email() + |> Email.render("password_reset_email.html", %{ + reset_link: "imaginary" + }) + + assert email.html_body =~ plausible_link() + end + + test "does not render unsubscribe placeholder" do + email = + Email.priority_email() + |> Email.render("password_reset_email.html", %{ + reset_link: "imaginary" + }) + + refute email.html_body =~ "{{{ pm:unsubscribe }}}" + end + + test "can be disabled with a nil layout" do + email = + Email.priority_email(%{layout: nil}) + |> Email.render("password_reset_email.html", %{ + reset_link: "imaginary" + }) + + refute email.html_body =~ "Hey John," + refute email.html_body =~ plausible_link() + end + end + def plausible_link() do plausible_url = PlausibleWeb.EmailView.plausible_url() "#{plausible_url}"