Skip to content

Commit

Permalink
autogenerate text emails
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Oct 14, 2024
1 parent 0c40042 commit e6d2663
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
38 changes: 35 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,39 @@ 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)
text = textify(html)

email
|> html_body(html)
|> text_body(text)
end

defp textify(html) do
html
|> Floki.parse_document!()
|> Floki.traverse_and_update(&textify_link/1)
|> 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 = IO.iodata_to_binary([text, " (", href, ?)])
{"p", attrs, [text_and_link]}

_ ->
node
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

0 comments on commit e6d2663

Please sign in to comment.