Skip to content
This repository
Browse code

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 Buck authored July 06, 2005
5  actionmailer/lib/action_mailer/base.rb
... ...
@@ -1,6 +1,7 @@
1 1
 require 'action_mailer/adv_attr_accessor'
2 2
 require 'action_mailer/part'
3 3
 require 'action_mailer/part_container'
  4
+require 'action_mailer/utils'
4 5
 require 'tmail/net'
5 6
 
6 7
 module ActionMailer #:nodoc:
@@ -293,11 +294,11 @@ def create_mail
293 294
 
294 295
         if @parts.empty?
295 296
           m.set_content_type content_type, nil, { "charset" => charset }
296  
-          m.body = body
  297
+          m.body = Utils.normalize_new_lines(body)
297 298
         else
298 299
           if String === body
299 300
             part = TMail::Mail.new
300  
-            part.body = body
  301
+            part.body = Utils.normalize_new_lines(body)
301 302
             part.set_content_type content_type, nil, { "charset" => charset }
302 303
             part.set_content_disposition "inline"
303 304
             m.parts << part
3  actionmailer/lib/action_mailer/part.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'action_mailer/adv_attr_accessor'
2 2
 require 'action_mailer/part_container'
  3
+require 'action_mailer/utils'
3 4
 
4 5
 module ActionMailer
5 6
   class Part #:nodoc:
@@ -29,7 +30,7 @@ def to_mail(defaults)
29 30
           when "base64" then
30 31
             part.body = TMail::Base64.folding_encode(body)
31 32
           when "quoted-printable"
32  
-            part.body = [body].pack("M*")
  33
+            part.body = [Utils.normalize_new_lines(body)].pack("M*")
33 34
           else
34 35
             part.body = body
35 36
         end
8  actionmailer/lib/action_mailer/utils.rb
... ...
@@ -0,0 +1,8 @@
  1
+module ActionMailer
  2
+  module Utils #:nodoc:
  3
+    def normalize_new_lines(text)
  4
+      text.to_s.gsub(/\r\n?/, "\n")
  5
+    end
  6
+    module_function :normalize_new_lines
  7
+  end
  8
+end
6  actionmailer/lib/action_mailer/vendor/tmail/mail.rb
@@ -335,13 +335,9 @@ def quoted_body
335 335
       }
336 336
     end
337 337
 
338  
-    def normalize_line_endings(text)
339  
-      text.to_s.gsub(/\r\n?/, "\n")
340  
-    end
341  
-
342 338
     def body=( str )
343 339
       parse_body
344  
-      @body_port.wopen {|f| f.write normalize_line_endings(str) }
  340
+      @body_port.wopen {|f| f.write str }
345 341
       str
346 342
     end
347 343
 
15  actionmailer/test/mail_service_test.rb
@@ -146,6 +146,15 @@ def various_newlines(recipient)
146 146
                  "line #5\n\nline#6\r\n\r\nline #7"
147 147
   end
148 148
 
  149
+  def various_newlines_multipart(recipient)
  150
+    recipients   recipient
  151
+    subject      "various newlines multipart"
  152
+    from         "test@example.com"
  153
+    content_type "multipart/alternative"
  154
+    part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r"
  155
+    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"
  156
+  end
  157
+
149 158
   def nested_multipart(recipient)
150 159
     recipients   recipient
151 160
     subject      "nested multipart"
@@ -597,6 +606,12 @@ def test_various_newlines
597 606
                  "line #5\n\nline#6\n\nline #7", mail.body)
598 607
   end
599 608
 
  609
+  def test_various_newlines_multipart
  610
+    mail = TestMailer.create_various_newlines_multipart(@recipient)
  611
+    assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body
  612
+    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
  613
+  end
  614
+  
600 615
   def test_headers_removed_on_smtp_delivery
601 616
     ActionMailer::Base.delivery_method = :smtp
602 617
     TestMailer.deliver_cc_bcc(@recipient)

0 notes on commit bde3df2

Please sign in to comment.
Something went wrong with that request. Please try again.