Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix attachment handling in ActionMailer::InlineCssHook.delivering_ema…

…il(message)
  • Loading branch information...
commit 43ebf9f20880bf43557fb79d9922495ee46421c7 1 parent 037a534
@leehambley leehambley authored
Showing with 37 additions and 15 deletions.
  1. +13 −13 lib/action_mailer/inline_css_hook.rb
  2. +24 −2 test/inline_css_hook_test.rb
View
26 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
View
26 test/inline_css_hook_test.rb
@@ -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
@@ -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
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.