Skip to content

Commit

Permalink
Support css background-image attributes with cid: on mail preview
Browse files Browse the repository at this point in the history
  • Loading branch information
le0pard committed Apr 24, 2024
1 parent 2c35684 commit 4783d41
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
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

0 comments on commit 4783d41

Please sign in to comment.