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

Autogenerate text emails #4674

Merged
merged 11 commits into from
Jan 2, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
### Changed
Expand Down
66 changes: 60 additions & 6 deletions lib/plausible_web/email.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -459,17 +459,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
ruslandoga marked this conversation as resolved.
Show resolved Hide resolved
{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
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
1 change: 0 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
3 changes: 3 additions & 0 deletions test/plausible/imported/csv_importer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
92 changes: 91 additions & 1 deletion test/plausible_web/email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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
Expand All @@ -22,6 +23,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
Expand All @@ -30,6 +32,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
Expand All @@ -50,6 +53,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

Expand All @@ -74,6 +80,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
Expand All @@ -84,6 +91,7 @@ defmodule PlausibleWeb.EmailTest do
})

assert email.html_body =~ "Hey,"
assert email.text_body =~ "Hey,"
end

test "renders plausible link" do
Expand All @@ -94,6 +102,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
Expand All @@ -115,6 +124,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

Expand Down Expand Up @@ -313,8 +325,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
1 change: 1 addition & 0 deletions test/workers/notify_exported_analytics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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
Loading