提交 bde3df2b 编写于 作者: J Jamis Buck

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
上级 853ea556
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
......
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
......
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
......@@ -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
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册