Sha256: a3a6df5c1fac6fb955505ed597b41fff41b83b11d24b0f33529a7885dea8833a

Contents?: true

Size: 1.39 KB

Versions: 76

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

76 entries across 76 versions & 7 rubygems

Version Path
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/actionmailer-6.0.2.1/lib/action_mailer/inline_preview_interceptor.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/actionmailer-6.0.2.1/lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.2.1 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.2 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.2.rc2 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.2.rc1 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.1 lib/action_mailer/inline_preview_interceptor.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/actionmailer-6.0.0/lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.1.rc1 lib/action_mailer/inline_preview_interceptor.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/actionmailer-6.0.0/lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.0 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.0.rc2 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.0.rc1 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.0.beta3 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.0.beta2 lib/action_mailer/inline_preview_interceptor.rb
actionmailer-6.0.0.beta1 lib/action_mailer/inline_preview_interceptor.rb