Sha256: db18d276e71ab0ccdc24c122ac13c68b0976117c504ad25e4c51f7d7ba8139a7

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

#
# Always inline CSS for HTML emails
#
module ActionMailer
  class InlineCssHook
    def self.delivering_email(message)

      if html_part = (message.html_part || (message.content_type =~ /text\/html/ && message))
        host = ActionMailerInlineCss.base_url || message.header[:host].to_s

        # Generate an email with all CSS inlined (access CSS a FS path), and URIs
        premailer = ::Premailer.new(html_part.body.to_s, :with_html_string => true, :base_url => host)

        msg_charset = message.charset

        if message.text_part && message.text_part.body.to_s
          html_part.content_type "text/html; charset=#{msg_charset}"
          html_part.body premailer.to_inline_css
        else
          existing_attachments = message.attachments

          # Clear body to make a multipart email
          message.body = nil

          # IMPORTANT: Plain text part must be generated before CSS is inlined.
          # Not doing so results in CSS declarations (<style>) visible in the plain text part.
          message.text_part = Mail::Part.new do
            content_type "text/plain; charset=#{msg_charset}"
            body premailer.to_plain_text
          end

          message.html_part = Mail::Part.new do
            content_type "text/html; charset=#{msg_charset}"
            body premailer.to_inline_css
          end

          message.content_type 'multipart/mixed' if ! existing_attachments.empty?

          existing_attachments.each {|a| message.body << a }
        end

        message
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
actionmailer_inline_css-1.6.0 lib/action_mailer/inline_css_hook.rb
actionmailer_inline_css-1.5.3 lib/action_mailer/inline_css_hook.rb