123456789_123456789_123456789_123456789_123456789_

Class: ActionMailer::InlinePreviewInterceptor

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Base64
Inherits: Object
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 an HTML email in a web browser.

This interceptor is enabled by default. To disable it, delete it from the Base.preview_interceptors array:

ActionMailer::Base.preview_interceptors.delete(ActionMailer::InlinePreviewInterceptor)

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(message) ⇒ InlinePreviewInterceptor

This method is for internal use only.
[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 26

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

Class Method Details

.previewing_email(message)

This method is for internal use only.
[ GitHub ]

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

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

Instance Attribute Details

#message (readonly, private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 45

attr_reader :message

Instance Method Details

#data_url(part) (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 51

def data_url(part)
  "data:#{part.mime_type};base64,#{strict_encode64(part.body.raw_source)}"
end

#find_part(cid) (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 55

def find_part(cid)
  message.all_parts.find { |p| p.attachment? && p.cid == cid }
end

#html_part (private)

[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 47

def html_part
  @html_part ||= message.html_part
end

#transform!

This method is for internal use only.
[ GitHub ]

  
# File 'actionmailer/lib/action_mailer/inline_preview_interceptor.rb', line 30

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)}"]
    else
      match
    end
  end

  message
end