Sha256: 07cbe0908c6e8fbf149a3ddb957ad4da392f2b9c886c5bfdb15674f8ec2b0b2f

Contents?: true

Size: 1.39 KB

Versions: 38

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

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 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

    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_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

    private
      attr_reader :message

      def html_part
        @html_part ||= message.html_part
      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

Version data entries

38 entries across 36 versions & 4 rubygems

Version Path
actionmailer-7.0.8.7 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.8.6 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.8.5 lib/action_mailer/inline_preview_interceptor.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionmailer-7.0.8.4/lib/action_mailer/inline_preview_interceptor.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionmailer-7.0.5.1/lib/action_mailer/inline_preview_interceptor.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionmailer-7.0.5.1/lib/action_mailer/inline_preview_interceptor.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionmailer-7.0.5.1/lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.8.4 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.8.1 lib/action_mailer/inline_preview_interceptor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionmailer-7.0.2.3/lib/action_mailer/inline_preview_interceptor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionmailer-7.0.3.1/lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.8 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.7.2 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.7.1 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.7 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.6 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.5.1 lib/action_mailer/inline_preview_interceptor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionmailer-7.0.3.1/lib/action_mailer/inline_preview_interceptor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionmailer-7.0.2.3/lib/action_mailer/inline_preview_interceptor.rb
actionmailer-7.0.5 lib/action_mailer/inline_preview_interceptor.rb