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 3 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
## Unreleased

### Added

- Add text version to emails plausible/analytics#4674

### Removed
### Changed
### Fixed
Expand Down
49 changes: 46 additions & 3 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 @@ -460,7 +460,50 @@ defmodule PlausibleWeb.Email do

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)

on_ee do
email |> html_body(html)
else
email |> html_body(html) |> text_body(textify(html))
end
end

on_ce do
defp textify(html) do
html
|> Floki.parse_document!()
|> Floki.traverse_and_update(&textify_link/1)
ruslandoga marked this conversation as resolved.
Show resolved Hide resolved
|> Floki.text()
end

defp textify_link(node) do
case node do
{"a", attrs, children} ->
{"href", href} = List.keyfind!(attrs, "href", 0)
text = Floki.text(children)

text_and_link =
if text == href do
# avoids rendering "http://localhost:8000 (http://localhost:8000)"
# e.g. in base_email footer
text
else
IO.iodata_to_binary([text, " (", href, ?)])
end

{"p", attrs, [text_and_link]}

_ ->
node
end
end
end
end
3 changes: 1 addition & 2 deletions lib/plausible_web/templates/email/export_success.html.heex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Your <%= Plausible.product_name() %> export for <%= @site.domain %> is now ready for download.
Please click <a href={@download_url}>here</a>
to start the download process.
<p>Please click <a href={@download_url}>here</a> to start the download process.</p>
ruslandoga marked this conversation as resolved.
Show resolved Hide resolved
<%= if @expires_in do %>
Note that this link will expire <%= @expires_in %>.
<% 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
5 changes: 5 additions & 0 deletions test/plausible/imported/csv_importer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ 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>]

if Plausible.ce?() do
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.]
end

# download archive
on_ee do
ExAws.request!(
Expand Down
40 changes: 39 additions & 1 deletion test/plausible_web/email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ defmodule PlausibleWeb.EmailTest do
})

assert email.html_body =~ "Hey John,"

if Plausible.ce?() do
assert email.text_body =~ "Hey John,"
end
end

test "greets impersonally when user not in template assigns" do
Expand All @@ -22,6 +26,10 @@ defmodule PlausibleWeb.EmailTest do
|> Email.render("welcome_email.html")

assert email.html_body =~ "Hey,"

if Plausible.ce?() do
assert email.text_body =~ "Hey,"
end
end

test "renders plausible link" do
Expand All @@ -30,6 +38,10 @@ defmodule PlausibleWeb.EmailTest do
|> Email.render("welcome_email.html")

assert email.html_body =~ plausible_link()

if Plausible.ce?() do
assert email.text_body =~ plausible_url()
end
end

@tag :ee_only
Expand All @@ -50,6 +62,11 @@ defmodule PlausibleWeb.EmailTest do

refute email.html_body =~ "Hey John,"
refute email.html_body =~ plausible_link()

if Plausible.ce?() do
refute email.text_body =~ "Hey John,"
refute email.text_body =~ plausible_url()
end
end
end

Expand All @@ -74,6 +91,10 @@ defmodule PlausibleWeb.EmailTest do
})

assert email.html_body =~ "Hey John,"

if Plausible.ce?() do
assert email.text_body =~ "Hey John,"
end
end

test "greets impersonally when user not in template assigns" do
Expand All @@ -84,6 +105,10 @@ defmodule PlausibleWeb.EmailTest do
})

assert email.html_body =~ "Hey,"

if Plausible.ce?() do
assert email.text_body =~ "Hey,"
end
end

test "renders plausible link" do
Expand All @@ -94,6 +119,10 @@ defmodule PlausibleWeb.EmailTest do
})

assert email.html_body =~ plausible_link()

if Plausible.ce?() do
assert email.text_body =~ plausible_url()
end
end

test "does not render unsubscribe placeholder" do
Expand All @@ -115,6 +144,11 @@ defmodule PlausibleWeb.EmailTest do

refute email.html_body =~ "Hey John,"
refute email.html_body =~ plausible_link()

if Plausible.ce?() do
refute email.text_body =~ "Hey John,"
refute email.text_body =~ plausible_url()
end
end
end

Expand Down Expand Up @@ -313,8 +347,12 @@ defmodule PlausibleWeb.EmailTest do
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
4 changes: 4 additions & 0 deletions test/workers/notify_exported_analytics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ defmodule Plausible.Workers.NotifyExportedAnalyticsTest do

assert_receive {:delivered_email, email}
assert email.html_body =~ "was unsuccessful."

if Plausible.ce?() do
assert email.text_body =~ "was unsuccessful."
end
end
end
end