From e49903f386f27edf7f5e34daf1de66e018a6edc6 Mon Sep 17 00:00:00 2001 From: Andreas Lappe Date: Tue, 28 Jun 2022 17:16:12 +0200 Subject: [PATCH] Display attached images if referenced in html --- .../plug/sent_email_viewer/email_preview_plug.ex | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/bamboo/plug/sent_email_viewer/email_preview_plug.ex b/lib/bamboo/plug/sent_email_viewer/email_preview_plug.ex index 2e421420..4862fe94 100644 --- a/lib/bamboo/plug/sent_email_viewer/email_preview_plug.ex +++ b/lib/bamboo/plug/sent_email_viewer/email_preview_plug.ex @@ -62,6 +62,8 @@ defmodule Bamboo.SentEmailViewerPlug do get "/:id/html" do if email = SentEmail.get(id) do + email = rewrite_html_body_cids(email) + conn |> Plug.Conn.put_resp_content_type("text/html") |> send_resp(:ok, email.html_body || "") @@ -125,4 +127,15 @@ defmodule Bamboo.SentEmailViewerPlug do defp base_path(%{script_name: script_name}) do "/" <> Enum.join(script_name, "/") end + + defp rewrite_html_body_cids(email) do + %{ email | html_body: email.html_body + |> String.replace(~r/cid:[^'"]+/, fn "cid:" <> id = cid -> + case Enum.find_index(email.attachments, &(&1.content_id == id)) do + nil -> cid + i -> "/sent_emails/#{email.private.local_adapter_id}/attachments/#{i}" + end + end) + } + end end