Correctly normalize newlines in outgoing emails before encoding the body [John Long]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1732 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck 2005-07-06 09:53:34 +00:00
parent 853ea55674
commit bde3df2bec
5 changed files with 29 additions and 8 deletions

View File

@ -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 @@ module ActionMailer #:nodoc:
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 File

@ -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 @@ module ActionMailer
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 File

@ -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 File

@ -335,13 +335,9 @@ module TMail
}
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 File

@ -146,6 +146,15 @@ class TestMailer < ActionMailer::Base
"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 @@ EOF
"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)