Skip to content

Commit

Permalink
Fix attachment handling in ActionMailer::InlineCssHook.delivering_ema…
Browse files Browse the repository at this point in the history
…il(message)
  • Loading branch information
leehambley committed Oct 24, 2011
1 parent 037a534 commit 43ebf9f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
26 changes: 13 additions & 13 deletions lib/action_mailer/inline_css_hook.rb
@@ -1,33 +1,33 @@
# encoding: utf-8
#
# 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))

# Generate an email with all CSS inlined (access CSS a FS path)
premailer = ::Premailer.new(html_part.body.to_s,
:with_html_string => true)
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
# Reset the body
message.body = nil
premailer = ::Premailer.new(premailer.to_inline_css, :with_html_string => true,
:base_url => message.header[:host].to_s)

# Add an HTML part with CSS inlined.
message.html_part do
content_type "text/html; charset=utf-8"
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=utf-8"
body existing_text_part || premailer.to_plain_text
end

end

# Return the message, ActionMailer doesn't seem to care, but it's useful
# for writing meaningful tests.
message

end
end
end
26 changes: 24 additions & 2 deletions test/inline_css_hook_test.rb
Expand Up @@ -57,6 +57,7 @@ def use_inline_css_hook_with_text_and_html_parts

def use_inline_css_hook_with_utf_8
mail_with_defaults do |format|
charset "utf8"
format.html { render(:inline => TEST_HTML_UTF8) }
end
end
Expand All @@ -67,12 +68,20 @@ def inline_css_hook_with_base_url
end
end

def with_attachment
mail_with_defaults do |format|
attachments["hello"] = File.read('test')
format.html { render(:inline => TEST_HTML) }
end
end

protected

def mail_with_defaults(&block)
mail(:to => "test@localhost", :from => "tester@example.com",
:subject => "using helpers", &block)
end

end


Expand All @@ -92,7 +101,10 @@ def test_inline_css_hook_with_text_and_html_parts
end

def test_inline_css_hook_with_utf_8_characters
mail = HelperMailer.use_inline_css_hook_with_utf_8.deliver
mail = nil
Kernel.silence_warnings do
mail = HelperMailer.use_inline_css_hook_with_utf_8.deliver
end
assert_match 'ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', mail.html_part.body.encoded
assert_match 'ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', mail.text_part.body.encoded
end
Expand All @@ -102,5 +114,15 @@ def test_inline_css_hook_with_base_url
assert_match '<img src="http://www.example.com/images/test.png">',
mail.html_part.body.encoded
end
end

def test_preservation_of_attachments
File.stubs(:read).returns("world")
mail = HelperMailer.with_attachment
assert mail.attachments["hello"].is_a?(Mail::Part)
original_hello_attachment_url = mail.attachments["hello"].url
m = ActionMailer::InlineCssHook.delivering_email(mail.deliver)
assert m.attachments["hello"].is_a?(Mail::Part)
assert_equal original_hello_attachment_url, mail.attachments["hello"].url
end

end

0 comments on commit 43ebf9f

Please sign in to comment.