Class: ActionMailer::InlineCssHook

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mailer/inline_css_hook.rb

Class Method Summary collapse

Class Method Details

.delivering_email(message) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/action_mailer/inline_css_hook.rb', line 6

def self.delivering_email(message)
  if html_part = (message.html_part || (message.content_type =~ /text\/html/ && message))
    # Generate an email with all CSS inlined (access CSS a FS path)
    premailer = ::Premailer.new(html_part.body.to_s,
                                :with_html_string => true)

    # Prepend host to remaning URIs.
    # Two-phase conversion to avoid request deadlock from dev. server (Issue #4)
    premailer = ::Premailer.new(premailer.to_inline_css,
                                  :with_html_string => true,
                                  :base_url => message.header[:host].to_s)
    existing_text_part = message.text_part && message.text_part.body.to_s
    existing_attachments = message.attachments
    msg_charset = message.charset

    # Reset the body
    message.body = nil
    message.body.instance_variable_set("@parts", Mail::PartsList.new)

    # Add an HTML part with CSS inlined.
    message.html_part do
      content_type "text/html; charset=#{msg_charset}"
      body premailer.to_inline_css
    end

    # Add a text part with either the pre-existing text part, or one generated with premailer.
    message.text_part do
      content_type "text/plain; charset=#{msg_charset}"
      body existing_text_part || premailer.to_plain_text
    end

    # Re-add any attachments
    existing_attachments.each {|a| message.body << a }

    message
  end
end