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

Support css background-image attributes with cid: on mail preview #51656

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions actionmailer/CHANGELOG.md
@@ -1,3 +1,7 @@
* Support css background-image attributes with cid: on mail preview

*Alexey Vasiliev*

* Remove deprecated params via `:args` for `assert_enqueued_email_with`.

*Rafael Mendonça França*
Expand Down
10 changes: 5 additions & 5 deletions actionmailer/lib/action_mailer/inline_preview_interceptor.rb
Expand Up @@ -6,16 +6,16 @@ module ActionMailer
# = Action Mailer \InlinePreviewInterceptor
#
# Implements a mailer preview interceptor that converts image tag src attributes
# that use inline cid: style URLs to data: style URLs so that they are visible
# when previewing an HTML email in a web browser.
# or css attributes that use inline cid: style URLs to data: style URLs
# so that they are visible when previewing an HTML email in a web browser.
#
# This interceptor is enabled by default. To disable it, delete it from the
# <tt>ActionMailer::Base.preview_interceptors</tt> array:
#
# ActionMailer::Base.preview_interceptors.delete(ActionMailer::InlinePreviewInterceptor)
#
class InlinePreviewInterceptor
PATTERN = /src=(?:"cid:[^"]+"|'cid:[^']+')/i
PATTERN = /src=(?:"cid:[^"]+"|'cid:[^']+')|url\((?:"cid:[^"]+"|'cid:[^']+'|cid:[^)]+)\)/i

include Base64

Expand All @@ -31,8 +31,8 @@ def transform! # :nodoc:
return message if html_part.blank?

html_part.body = html_part.decoded.gsub(PATTERN) do |match|
if part = find_part(match[9..-2])
%[src="#{data_url(part)}"]
if part = find_part(match.match(/cid:([^"')]+)/i)[1])
match.start_with?("src=") ? %[src="#{data_url(part)}"] : %[url(#{data_url(part)})]
else
match
end
Expand Down