Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Autogenerate text emails (#4674)

ruslandoga6ed846395f08
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d6c59f12ec..62a4977888 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
- Dashboard shows comparisons for all reports
- UTM Medium report and API shows (gclid) and (msclkid) for paid searches when no explicit utm medium present.
- Support for `case_sensitive: false` modifiers in Stats API V2 filters for case-insensitive searches.
+- Add text version to emails plausible/analytics#4674
### Removed
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 2303da96e3..1a38fb8c7e 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -1,6 +1,6 @@
defmodule PlausibleWeb.Email do
use Plausible
- use Bamboo.Phoenix, view: PlausibleWeb.EmailView
+ import Bamboo.Email
import Bamboo.PostmarkHelper
def mailer_email_from do
@@ -460,17 +460,71 @@ defmodule PlausibleWeb.Email do
def base_email(), do: base_email(%{layout: "base_email.html"})
def base_email(%{layout: layout}) do
- mailer_from = Application.get_env(:plausible, :mailer_email)
-
new_email()
|> put_param("TrackOpens", false)
- |> from(mailer_from)
+ |> from(mailer_email_from())
|> maybe_put_layout(layout)
end
defp maybe_put_layout(email, nil), do: email
- defp maybe_put_layout(email, layout) do
- put_html_layout(email, {PlausibleWeb.LayoutView, layout})
+ defp maybe_put_layout(%{assigns: assigns} = email, layout) do
+ %{email | assigns: Map.put(assigns, :layout, {PlausibleWeb.LayoutView, layout})}
+ end
+
+ @doc false
+ def render(email, template, assigns \\ []) do
+ assigns = Map.merge(email.assigns, Map.new(assigns))
+ html = Phoenix.View.render_to_string(PlausibleWeb.EmailView, template, assigns)
+ email |> html_body(html) |> text_body(textify(html))
+ end
+
+ defp textify(html) do
+ Floki.parse_fragment!(html)
+ |> traverse_and_textify()
+ |> Floki.text()
+ |> collapse_whitespace()
+ end
+
+ defp traverse_and_textify([head | tail]) do
+ [traverse_and_textify(head) | traverse_and_textify(tail)]
+ end
+
+ defp traverse_and_textify(text) when is_binary(text) do
+ String.replace(text, "\n", "\s")
+ end
+
+ defp traverse_and_textify({"a" = tag, attrs, children}) do
+ href = with {"href", href} <- List.keyfind(attrs, "href", 0), do: href
+ children = traverse_and_textify(children)
+
+ if href do
+ text = Floki.text(children)
+
+ if text == href do
+ # avoids rendering "http://localhost:8000 (http://localhost:8000)" in base_email footer
+ text
+ else
+ IO.iodata_to_binary([text, " (", href, ?)])
+ end
+ else
+ {tag, attrs, children}
+ end
+ end
+
+ defp traverse_and_textify({tag, attrs, children}) do
+ {tag, attrs, traverse_and_textify(children)}
+ end
+
+ defp traverse_and_textify(other), do: other
+
+ defp collapse_whitespace(text) do
+ text
+ |> String.split("\n")
+ |> Enum.map_join("\n", fn line ->
+ line
+ |> String.split(" ", trim: true)
+ |> Enum.join(" ")
+ end)
end
end
diff --git a/mix.exs b/mix.exs
index 5a115b9c6d..bff10a52e8 100644
--- a/mix.exs
+++ b/mix.exs
@@ -63,7 +63,6 @@ defmodule Plausible.MixProject do
defp deps do
[
{:bamboo, "~> 2.3", override: true},
- {:bamboo_phoenix, "~> 1.0.0"},
{:bamboo_postmark, git: "https://github.com/plausible/bamboo_postmark.git", branch: "main"},
{:bamboo_smtp, "~> 4.1"},
{:bamboo_mua, "~> 0.2.0"},
diff --git a/mix.lock b/mix.lock
index 9ced0d73a8..59456e4ff9 100644
--- a/mix.lock
+++ b/mix.lock
@@ -2,7 +2,6 @@
"acceptor_pool": {:hex, :acceptor_pool, "1.0.0", "43c20d2acae35f0c2bcd64f9d2bde267e459f0f3fd23dab26485bf518c281b21", [:rebar3], [], "hexpm", "0cbcd83fdc8b9ad2eee2067ef8b91a14858a5883cb7cd800e6fcd5803e158788"},
"bamboo": {:hex, :bamboo, "2.3.0", "d2392a2cabe91edf488553d3c70638b532e8db7b76b84b0a39e3dfe492ffd6fc", [:mix], [{:hackney, ">= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.4 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "dd0037e68e108fd04d0e8773921512c940e35d981e097b5793543e3b2f9cd3f6"},
"bamboo_mua": {:hex, :bamboo_mua, "0.2.2", "c50cd41ef684155669e2d99d428fbb87e13797a80829a162b47d3c0a7f7e7ecd", [:mix], [{:bamboo, "~> 2.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:mail, "~> 0.3.0", [hex: :mail, repo: "hexpm", optional: false]}, {:mua, "~> 0.2.3", [hex: :mua, repo: "hexpm", optional: false]}], "hexpm", "5fe6e3676640578c6fe8f040b34dda8991ebef8566c0601a984eb4771b85b11f"},
- "bamboo_phoenix": {:hex, :bamboo_phoenix, "1.0.0", "f3cc591ffb163ed0bf935d256f1f4645cd870cf436545601215745fb9cc9953f", [:mix], [{:bamboo, ">= 2.0.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.3.0", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "6db88fbb26019c84a47994bb2bd879c0887c29ce6c559bc6385fd54eb8b37dee"},
"bamboo_postmark": {:git, "https://github.com/plausible/bamboo_postmark.git", "5eac6dfdffacd273bd9aacdd3e494a600bb0b170", [branch: "main"]},
"bamboo_smtp": {:hex, :bamboo_smtp, "4.2.2", "e9f57a2300df9cb496c48751bd7668a86a2b89aa2e79ccaa34e0c46a5f64c3ae", [:mix], [{:bamboo, "~> 2.2.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:gen_smtp, "~> 1.2.0", [hex: :gen_smtp, repo: "hexpm", optional: false]}], "hexpm", "28cac2ec8adaae02aed663bf68163992891a3b44cfd7ada0bebe3e09bed7207f"},
"bcrypt_elixir": {:hex, :bcrypt_elixir, "3.1.0", "0b110a9a6c619b19a7f73fa3004aa11d6e719a67e672d1633dc36b6b2290a0f7", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "2ad2acb5a8bc049e8d5aa267802631912bb80d5f4110a178ae7999e69dca1bf7"},
diff --git a/test/plausible/imported/csv_importer_test.exs b/test/plausible/imported/csv_importer_test.exs
index ce06ff8dac..7e5831a952 100644
--- a/test/plausible/imported/csv_importer_test.exs
+++ b/test/plausible/imported/csv_importer_test.exs
@@ -542,6 +542,9 @@ defmodule Plausible.Imported.CSVImporterTest do
assert email.html_body =~
~s[Please click <a href="http://localhost:8000/#{URI.encode_www_form(exported_site.domain)}/download/export">here</a>]
+ assert email.text_body =~
+ ~r[Please click here \(http://localhost:8000/#{URI.encode_www_form(exported_site.domain)}/download/export\) to start the download process.]
+
# download archive
on_ee do
ExAws.request!(
diff --git a/test/plausible_web/email_test.exs b/test/plausible_web/email_test.exs
index eabbb25a93..0df984601b 100644
--- a/test/plausible_web/email_test.exs
+++ b/test/plausible_web/email_test.exs
@@ -17,6 +17,7 @@ defmodule PlausibleWeb.EmailTest do
})
assert email.html_body =~ "Hey John,"
+ assert email.text_body =~ "Hey John,"
end
test "greets impersonally when user not in template assigns" do
@@ -25,6 +26,7 @@ defmodule PlausibleWeb.EmailTest do
|> Email.render("welcome_email.html")
assert email.html_body =~ "Hey,"
+ assert email.text_body =~ "Hey,"
end
test "renders plausible link" do
@@ -33,6 +35,7 @@ defmodule PlausibleWeb.EmailTest do
|> Email.render("welcome_email.html")
assert email.html_body =~ plausible_link()
+ assert email.text_body =~ plausible_url()
end
@tag :ee_only
@@ -53,6 +56,9 @@ defmodule PlausibleWeb.EmailTest do
refute email.html_body =~ "Hey John,"
refute email.html_body =~ plausible_link()
+
+ refute email.text_body =~ "Hey John,"
+ refute email.text_body =~ plausible_url()
end
end
@@ -77,6 +83,7 @@ defmodule PlausibleWeb.EmailTest do
})
assert email.html_body =~ "Hey John,"
+ assert email.text_body =~ "Hey John,"
end
test "greets impersonally when user not in template assigns" do
@@ -87,6 +94,7 @@ defmodule PlausibleWeb.EmailTest do
})
assert email.html_body =~ "Hey,"
+ assert email.text_body =~ "Hey,"
end
test "renders plausible link" do
@@ -97,6 +105,7 @@ defmodule PlausibleWeb.EmailTest do
})
assert email.html_body =~ plausible_link()
+ assert email.text_body =~ plausible_url()
end
test "does not render unsubscribe placeholder" do
@@ -118,6 +127,9 @@ defmodule PlausibleWeb.EmailTest do
refute email.html_body =~ "Hey John,"
refute email.html_body =~ plausible_link()
+
+ refute email.text_body =~ "Hey John,"
+ refute email.text_body =~ plausible_url()
end
end
@@ -323,8 +335,86 @@ defmodule PlausibleWeb.EmailTest do
end
end
+ describe "text_body" do
+ @tag :ee_only
+ test "welcome_email (EE)" do
+ email =
+ Email.base_email()
+ |> Email.render("welcome_email.html", %{
+ user: build(:user, name: "John Doe"),
+ code: "123"
+ })
+
+ assert email.text_body == """
+ Hey John,
+
+ We are building Plausible to provide a simple and ethical approach to tracking website visitors. We're super excited to have you on board!
+
+ Here's how to get the most out of your Plausible experience:
+
+ * Enable email reports (https://plausible.io/docs/email-reports) and notifications for traffic spikes (https://plausible.io/docs/traffic-spikes)
+ * Integrate with Search Console (https://plausible.io/docs/google-search-console-integration) to get keyword phrases people find your site with
+ * Invite team members and other collaborators (https://plausible.io/docs/users-roles)
+ * Set up easy goals including 404 error pages (https://plausible.io/docs/error-pages-tracking-404), file downloads (https://plausible.io/docs/file-downloads-tracking) and outbound link clicks (https://plausible.io/docs/outbound-link-click-tracking)
+ * Opt out from counting your own visits (https://plausible.io/docs/excluding)
+ * If you're concerned about adblockers, set up a proxy to bypass them (https://plausible.io/docs/proxy/introduction)
+
+
+ Then you're ready to start exploring your fast loading, ethical and actionable Plausible dashboard (https://plausible.io/sites).
+
+ Have a question, feedback or need some guidance? Do reply back to this email.
+
+ Regards,
+ The Plausible Team 💌
+
+ --
+
+ http://localhost:8000
+ {{{ pm:unsubscribe }}}\
+ """
+ end
+
+ @tag :ce_build_only
+ test "welcome_email (CE)" do
+ email =
+ Email.base_email()
+ |> Email.render("welcome_email.html", %{
+ user: build(:user, name: "John Doe"),
+ code: "123"
+ })
+
+ assert email.text_body == """
+ Hey John,
+
+ We are building Plausible to provide a simple and ethical approach to tracking website visitors. We're super excited to have you on board!
+
+ Here's how to get the most out of your Plausible experience:
+
+ * Enable email reports (https://plausible.io/docs/email-reports) and notifications for traffic spikes (https://plausible.io/docs/traffic-spikes)
+ * Integrate with Search Console (https://plausible.io/docs/google-search-console-integration) to get keyword phrases people find your site with
+ * Invite team members and other collaborators (https://plausible.io/docs/users-roles)
+ * Set up easy goals including 404 error pages (https://plausible.io/docs/error-pages-tracking-404), file downloads (https://plausible.io/docs/file-downloads-tracking) and outbound link clicks (https://plausible.io/docs/outbound-link-click-tracking)
+ * Opt out from counting your own visits (https://plausible.io/docs/excluding)
+ * If you're concerned about adblockers, set up a proxy to bypass them (https://plausible.io/docs/proxy/introduction)
+
+
+ Then you're ready to start exploring your fast loading, ethical and actionable Plausible dashboard (https://plausible.io/sites).
+
+ Have a question, feedback or need some guidance? Do reply back to this email.
+
+ --
+
+ http://localhost:8000
+ """
+ end
+ end
+
+ def plausible_url do
+ PlausibleWeb.EmailView.plausible_url()
+ end
+
def plausible_link() do
- plausible_url = PlausibleWeb.EmailView.plausible_url()
+ plausible_url = plausible_url()
"<a href=\"#{plausible_url}\">#{plausible_url}</a>"
end
end
diff --git a/test/workers/notify_exported_analytics_test.exs b/test/workers/notify_exported_analytics_test.exs
index 502f8f416a..ae34787901 100644
--- a/test/workers/notify_exported_analytics_test.exs
+++ b/test/workers/notify_exported_analytics_test.exs
@@ -28,6 +28,7 @@ defmodule Plausible.Workers.NotifyExportedAnalyticsTest do
assert_receive {:delivered_email, email}
assert email.html_body =~ "was unsuccessful."
+ assert email.text_body =~ "was unsuccessful."
end
end
end