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

feat: Display attached images if referenced in html #639

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions lib/bamboo/plug/sent_email_viewer/email_preview_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 || "")
Expand Down Expand Up @@ -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
Loading