Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Correctly normalize newlines in outgoing emails before encoding the b…

…ody [John Long]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1732 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit bde3df2bec99103eeb91c4caaf0483aacadccd92 1 parent 853ea55
@jamis jamis authored
View
5 actionmailer/lib/action_mailer/base.rb
@@ -1,6 +1,7 @@
require 'action_mailer/adv_attr_accessor'
require 'action_mailer/part'
require 'action_mailer/part_container'
+require 'action_mailer/utils'
require 'tmail/net'
module ActionMailer #:nodoc:
@@ -293,11 +294,11 @@ def create_mail
if @parts.empty?
m.set_content_type content_type, nil, { "charset" => charset }
- m.body = body
+ m.body = Utils.normalize_new_lines(body)
else
if String === body
part = TMail::Mail.new
- part.body = body
+ part.body = Utils.normalize_new_lines(body)
part.set_content_type content_type, nil, { "charset" => charset }
part.set_content_disposition "inline"
m.parts << part
View
3  actionmailer/lib/action_mailer/part.rb
@@ -1,5 +1,6 @@
require 'action_mailer/adv_attr_accessor'
require 'action_mailer/part_container'
+require 'action_mailer/utils'
module ActionMailer
class Part #:nodoc:
@@ -29,7 +30,7 @@ def to_mail(defaults)
when "base64" then
part.body = TMail::Base64.folding_encode(body)
when "quoted-printable"
- part.body = [body].pack("M*")
+ part.body = [Utils.normalize_new_lines(body)].pack("M*")
else
part.body = body
end
View
8 actionmailer/lib/action_mailer/utils.rb
@@ -0,0 +1,8 @@
+module ActionMailer
+ module Utils #:nodoc:
+ def normalize_new_lines(text)
+ text.to_s.gsub(/\r\n?/, "\n")
+ end
+ module_function :normalize_new_lines
+ end
+end
View
6 actionmailer/lib/action_mailer/vendor/tmail/mail.rb
@@ -335,13 +335,9 @@ def quoted_body
}
end
- def normalize_line_endings(text)
- text.to_s.gsub(/\r\n?/, "\n")
- end
-
def body=( str )
parse_body
- @body_port.wopen {|f| f.write normalize_line_endings(str) }
+ @body_port.wopen {|f| f.write str }
str
end
View
15 actionmailer/test/mail_service_test.rb
@@ -146,6 +146,15 @@ def various_newlines(recipient)
"line #5\n\nline#6\r\n\r\nline #7"
end
+ def various_newlines_multipart(recipient)
+ recipients recipient
+ subject "various newlines multipart"
+ from "test@example.com"
+ content_type "multipart/alternative"
+ part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r"
+ part :content_type => "text/html", :body => "<p>line #1</p>\n<p>line #2</p>\r<p>line #3</p>\r\n<p>line #4</p>\r\r"
+ end
+
def nested_multipart(recipient)
recipients recipient
subject "nested multipart"
@@ -597,6 +606,12 @@ def test_various_newlines
"line #5\n\nline#6\n\nline #7", mail.body)
end
+ def test_various_newlines_multipart
+ mail = TestMailer.create_various_newlines_multipart(@recipient)
+ assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body
+ assert_equal "<p>line #1</p>\n<p>line #2</p>\n<p>line #3</p>\n<p>line #4</p>\n\n", mail.parts[1].body
+ end
+
def test_headers_removed_on_smtp_delivery
ActionMailer::Base.delivery_method = :smtp
TestMailer.deliver_cc_bcc(@recipient)
Please sign in to comment.
Something went wrong with that request. Please try again.