Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for inline images to mailer previews
Use a preview interceptor to search for inline cid: urls in src attributes and convert them to data urls. Not enabled by default to ensure that we don't break any existing workflows.
- Loading branch information
Showing
5 changed files
with
84 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
actionmailer/lib/action_mailer/inline_preview_interceptor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'base64' | ||
|
||
module ActionMailer | ||
# 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 a HTML email in a web browser. | ||
# | ||
# This interceptor is not enabled by default, to use it just register it like any | ||
# other mailer preview interceptor: | ||
# | ||
# ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor) | ||
# | ||
class InlinePreviewInterceptor | ||
PATTERN = /src=(?:"cid:[^"]+"|'cid:[^']+')/i | ||
|
||
include Base64 | ||
|
||
def self.previewing_email(message) #:nodoc: | ||
new(message).transform! | ||
end | ||
|
||
def initialize(message) #:nodoc: | ||
@message = message | ||
end | ||
|
||
def transform! #:nodoc: | ||
return message if html_part.blank? | ||
|
||
html_source.gsub!(PATTERN) do |match| | ||
if part = find_part(match[9..-2]) | ||
%[src="#{data_url(part)}"] | ||
else | ||
match | ||
end | ||
end | ||
|
||
message | ||
end | ||
|
||
private | ||
def message | ||
@message | ||
end | ||
|
||
def html_part | ||
@html_part ||= message.html_part | ||
end | ||
|
||
def html_source | ||
html_part.body.raw_source | ||
end | ||
|
||
def data_url(part) | ||
"data:#{part.mime_type};base64,#{strict_encode64(part.body.raw_source)}" | ||
end | ||
|
||
def find_part(cid) | ||
message.all_parts.find{ |p| p.attachment? && p.cid == cid } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters