Skip to content

Commit 68a99e0

Browse files
committed
Autogenerate text emails (#4674)
* autogenerate text emails * fix export email formatting * fewer changes * add full text_body test * eh * cleanup * explain * remove recursive collapse_whitespace * remove comment
1 parent 66a2286 commit 68a99e0

File tree

7 files changed

+158
-9
lines changed

7 files changed

+158
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
## Unreleased
55

66
### Added
7+
8+
- Add text version to emails plausible/analytics#4674
9+
710
### Removed
811
### Changed
912
### Fixed

lib/plausible_web/email.ex

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule PlausibleWeb.Email do
22
use Plausible
3-
use Bamboo.Phoenix, view: PlausibleWeb.EmailView
3+
import Bamboo.Email
44
import Bamboo.PostmarkHelper
55

66
def mailer_email_from do
@@ -450,17 +450,71 @@ defmodule PlausibleWeb.Email do
450450
def base_email(), do: base_email(%{layout: "base_email.html"})
451451

452452
def base_email(%{layout: layout}) do
453-
mailer_from = Application.get_env(:plausible, :mailer_email)
454-
455453
new_email()
456454
|> put_param("TrackOpens", false)
457-
|> from(mailer_from)
455+
|> from(mailer_email_from())
458456
|> maybe_put_layout(layout)
459457
end
460458

461459
defp maybe_put_layout(email, nil), do: email
462460

463-
defp maybe_put_layout(email, layout) do
464-
put_html_layout(email, {PlausibleWeb.LayoutView, layout})
461+
defp maybe_put_layout(%{assigns: assigns} = email, layout) do
462+
%{email | assigns: Map.put(assigns, :layout, {PlausibleWeb.LayoutView, layout})}
463+
end
464+
465+
@doc false
466+
def render(email, template, assigns \\ []) do
467+
assigns = Map.merge(email.assigns, Map.new(assigns))
468+
html = Phoenix.View.render_to_string(PlausibleWeb.EmailView, template, assigns)
469+
email |> html_body(html) |> text_body(textify(html))
470+
end
471+
472+
defp textify(html) do
473+
Floki.parse_fragment!(html)
474+
|> traverse_and_textify()
475+
|> Floki.text()
476+
|> collapse_whitespace()
477+
end
478+
479+
defp traverse_and_textify([head | tail]) do
480+
[traverse_and_textify(head) | traverse_and_textify(tail)]
481+
end
482+
483+
defp traverse_and_textify(text) when is_binary(text) do
484+
String.replace(text, "\n", "\s")
485+
end
486+
487+
defp traverse_and_textify({"a" = tag, attrs, children}) do
488+
href = with {"href", href} <- List.keyfind(attrs, "href", 0), do: href
489+
children = traverse_and_textify(children)
490+
491+
if href do
492+
text = Floki.text(children)
493+
494+
if text == href do
495+
# avoids rendering "http://localhost:8000 (http://localhost:8000)" in base_email footer
496+
text
497+
else
498+
IO.iodata_to_binary([text, " (", href, ?)])
499+
end
500+
else
501+
{tag, attrs, children}
502+
end
503+
end
504+
505+
defp traverse_and_textify({tag, attrs, children}) do
506+
{tag, attrs, traverse_and_textify(children)}
507+
end
508+
509+
defp traverse_and_textify(other), do: other
510+
511+
defp collapse_whitespace(text) do
512+
text
513+
|> String.split("\n")
514+
|> Enum.map_join("\n", fn line ->
515+
line
516+
|> String.split(" ", trim: true)
517+
|> Enum.join(" ")
518+
end)
465519
end
466520
end

mix.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ defmodule Plausible.MixProject do
6363
defp deps do
6464
[
6565
{:bamboo, "~> 2.3", override: true},
66-
{:bamboo_phoenix, "~> 1.0.0"},
6766
{:bamboo_postmark, git: "https://github.com/plausible/bamboo_postmark.git", branch: "main"},
6867
{:bamboo_smtp, "~> 4.1"},
6968
{:bamboo_mua, "~> 0.2.0"},

mix.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"acceptor_pool": {:hex, :acceptor_pool, "1.0.0", "43c20d2acae35f0c2bcd64f9d2bde267e459f0f3fd23dab26485bf518c281b21", [:rebar3], [], "hexpm", "0cbcd83fdc8b9ad2eee2067ef8b91a14858a5883cb7cd800e6fcd5803e158788"},
33
"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"},
44
"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"},
5-
"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"},
65
"bamboo_postmark": {:git, "https://github.com/plausible/bamboo_postmark.git", "5eac6dfdffacd273bd9aacdd3e494a600bb0b170", [branch: "main"]},
76
"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"},
87
"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"},

test/plausible/imported/csv_importer_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,9 @@ defmodule Plausible.Imported.CSVImporterTest do
541541
assert email.html_body =~
542542
~s[Please click <a href="http://localhost:8000/#{URI.encode_www_form(exported_site.domain)}/download/export">here</a>]
543543

544+
assert email.text_body =~
545+
~r[Please click here \(http://localhost:8000/#{URI.encode_www_form(exported_site.domain)}/download/export\) to start the download process.]
546+
544547
# download archive
545548
on_ee do
546549
ExAws.request!(

test/plausible_web/email_test.exs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule PlausibleWeb.EmailTest do
1414
})
1515

1616
assert email.html_body =~ "Hey John,"
17+
assert email.text_body =~ "Hey John,"
1718
end
1819

1920
test "greets impersonally when user not in template assigns" do
@@ -22,6 +23,7 @@ defmodule PlausibleWeb.EmailTest do
2223
|> Email.render("welcome_email.html")
2324

2425
assert email.html_body =~ "Hey,"
26+
assert email.text_body =~ "Hey,"
2527
end
2628

2729
test "renders plausible link" do
@@ -30,6 +32,7 @@ defmodule PlausibleWeb.EmailTest do
3032
|> Email.render("welcome_email.html")
3133

3234
assert email.html_body =~ plausible_link()
35+
assert email.text_body =~ plausible_url()
3336
end
3437

3538
@tag :ee_only
@@ -50,6 +53,9 @@ defmodule PlausibleWeb.EmailTest do
5053

5154
refute email.html_body =~ "Hey John,"
5255
refute email.html_body =~ plausible_link()
56+
57+
refute email.text_body =~ "Hey John,"
58+
refute email.text_body =~ plausible_url()
5359
end
5460
end
5561

@@ -74,6 +80,7 @@ defmodule PlausibleWeb.EmailTest do
7480
})
7581

7682
assert email.html_body =~ "Hey John,"
83+
assert email.text_body =~ "Hey John,"
7784
end
7885

7986
test "greets impersonally when user not in template assigns" do
@@ -84,6 +91,7 @@ defmodule PlausibleWeb.EmailTest do
8491
})
8592

8693
assert email.html_body =~ "Hey,"
94+
assert email.text_body =~ "Hey,"
8795
end
8896

8997
test "renders plausible link" do
@@ -94,6 +102,7 @@ defmodule PlausibleWeb.EmailTest do
94102
})
95103

96104
assert email.html_body =~ plausible_link()
105+
assert email.text_body =~ plausible_url()
97106
end
98107

99108
test "does not render unsubscribe placeholder" do
@@ -115,6 +124,9 @@ defmodule PlausibleWeb.EmailTest do
115124

116125
refute email.html_body =~ "Hey John,"
117126
refute email.html_body =~ plausible_link()
127+
128+
refute email.text_body =~ "Hey John,"
129+
refute email.text_body =~ plausible_url()
118130
end
119131
end
120132

@@ -326,8 +338,86 @@ defmodule PlausibleWeb.EmailTest do
326338
end
327339
end
328340

341+
describe "text_body" do
342+
@tag :ee_only
343+
test "welcome_email (EE)" do
344+
email =
345+
Email.base_email()
346+
|> Email.render("welcome_email.html", %{
347+
user: build(:user, name: "John Doe"),
348+
code: "123"
349+
})
350+
351+
assert email.text_body == """
352+
Hey John,
353+
354+
We are building Plausible to provide a simple and ethical approach to tracking website visitors. We're super excited to have you on board!
355+
356+
Here's how to get the most out of your Plausible experience:
357+
358+
* Enable email reports (https://plausible.io/docs/email-reports) and notifications for traffic spikes (https://plausible.io/docs/traffic-spikes)
359+
* Integrate with Search Console (https://plausible.io/docs/google-search-console-integration) to get keyword phrases people find your site with
360+
* Invite team members and other collaborators (https://plausible.io/docs/users-roles)
361+
* 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)
362+
* Opt out from counting your own visits (https://plausible.io/docs/excluding)
363+
* If you're concerned about adblockers, set up a proxy to bypass them (https://plausible.io/docs/proxy/introduction)
364+
365+
366+
Then you're ready to start exploring your fast loading, ethical and actionable Plausible dashboard (https://plausible.io/sites).
367+
368+
Have a question, feedback or need some guidance? Do reply back to this email.
369+
370+
Regards,
371+
The Plausible Team 💌
372+
373+
--
374+
375+
http://localhost:8000
376+
{{{ pm:unsubscribe }}}\
377+
"""
378+
end
379+
380+
@tag :ce_build_only
381+
test "welcome_email (CE)" do
382+
email =
383+
Email.base_email()
384+
|> Email.render("welcome_email.html", %{
385+
user: build(:user, name: "John Doe"),
386+
code: "123"
387+
})
388+
389+
assert email.text_body == """
390+
Hey John,
391+
392+
We are building Plausible to provide a simple and ethical approach to tracking website visitors. We're super excited to have you on board!
393+
394+
Here's how to get the most out of your Plausible experience:
395+
396+
* Enable email reports (https://plausible.io/docs/email-reports) and notifications for traffic spikes (https://plausible.io/docs/traffic-spikes)
397+
* Integrate with Search Console (https://plausible.io/docs/google-search-console-integration) to get keyword phrases people find your site with
398+
* Invite team members and other collaborators (https://plausible.io/docs/users-roles)
399+
* 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)
400+
* Opt out from counting your own visits (https://plausible.io/docs/excluding)
401+
* If you're concerned about adblockers, set up a proxy to bypass them (https://plausible.io/docs/proxy/introduction)
402+
403+
404+
Then you're ready to start exploring your fast loading, ethical and actionable Plausible dashboard (https://plausible.io/sites).
405+
406+
Have a question, feedback or need some guidance? Do reply back to this email.
407+
408+
--
409+
410+
http://localhost:8000
411+
"""
412+
end
413+
end
414+
415+
def plausible_url do
416+
PlausibleWeb.EmailView.plausible_url()
417+
end
418+
329419
def plausible_link() do
330-
plausible_url = PlausibleWeb.EmailView.plausible_url()
420+
plausible_url = plausible_url()
331421
"<a href=\"#{plausible_url}\">#{plausible_url}</a>"
332422
end
333423
end

test/workers/notify_exported_analytics_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ defmodule Plausible.Workers.NotifyExportedAnalyticsTest do
2727

2828
assert_receive {:delivered_email, email}
2929
assert email.html_body =~ "was unsuccessful."
30+
assert email.text_body =~ "was unsuccessful."
3031
end
3132
end
3233
end

0 commit comments

Comments
 (0)