Class: ActionMailer::InlinePreviewInterceptor

Inherits:
Object
  • Object
show all
Includes:
Base64
Defined in:
actionmailer/lib/action_mailer/inline_preview_interceptor.rb

Overview

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)

Constant Summary collapse

PATTERN =
/src=(?:"cid:[^"]+"|'cid:[^']+')/i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ InlinePreviewInterceptor

:nodoc:


22
23
24
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 22

def initialize(message) #:nodoc:
  @message = message
end

Class Method Details

.previewing_email(message) ⇒ Object

:nodoc:


18
19
20
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 18

def self.previewing_email(message) #:nodoc:
  new(message).transform!
end

Instance Method Details

#transform!Object

:nodoc:


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 26

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