提交 100fd726 编写于 作者: J Jamis Buck

Added a "content_type" accessor to allow messages to explicitly specify a...

Added a "content_type" accessor to allow messages to explicitly specify a content-type other than "text/plain" (the default).


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1382 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 887497b0
*SVN*
* Added 'content_type' accessor, to allow content type to be set on a per-message basis. content_type defaults to "text/plain".
* Silently ignore Iconv::IllegalSequence errors when converting text #1341 [lon@speedymac.com]
* Support attachments and multipart messages.
......
......@@ -138,8 +138,11 @@ class Base
@@default_charset = "utf-8"
cattr_accessor :default_charset
@@default_content_type = "text/plain"
cattr_accessor :default_content_type
adv_attr_accessor :recipients, :subject, :body, :from, :sent_on, :headers,
:bcc, :cc, :charset
:bcc, :cc, :charset, :content_type
attr_reader :mail
......@@ -156,6 +159,7 @@ def initialize(method_name=nil, *parameters)
def create!(method_name, *parameters)
@bcc = @cc = @from = @recipients = @sent_on = @subject = nil
@charset = @@default_charset.dup
@content_type = @@default_content_type.dup
@parts = []
@headers = {}
@body = {}
......@@ -255,13 +259,13 @@ def create_mail
headers.each { |k, v| m[k] = v }
if @parts.empty?
m.set_content_type "text", "plain", { "charset" => charset }
m.set_content_type content_type, nil, { "charset" => charset }
m.body = body
else
if String === body
part = TMail::Mail.new
part.body = body
part.set_content_type "text", "plain", { "charset" => charset }
part.set_content_type content_type, nil, { "charset" => charset }
part.set_content_disposition "inline"
m.parts << part
end
......
......@@ -9,7 +9,7 @@ class Part #:nodoc:
adv_attr_accessor :filename, :transfer_encoding, :headers
def initialize(params)
@content_type = params[:content_type] || "text/plain"
@content_type = params[:content_type]
@content_disposition = params[:disposition] || "inline"
@charset = params[:charset]
@body = params[:body]
......@@ -20,7 +20,7 @@ def initialize(params)
def to_mail(defaults)
part = TMail::Mail.new
part.set_content_type(content_type, nil,
part.set_content_type(content_type || defaults.content_type, nil,
"charset" => (content_disposition == "attachment" ?
nil : (charset || defaults.charset)),
"name" => filename)
......
......@@ -98,6 +98,14 @@ def implicitly_multipart_example(recipient)
@body = { "recipient" => recipient }
end
def html_mail(recipient)
recipients recipient
subject "html mail"
from "test@example.com"
body "<em>Emphasize</em> <strong>this</strong>"
content_type "text/html"
end
class <<self
attr_accessor :received_body
end
......@@ -454,5 +462,10 @@ def test_implicitly_multipart_messages
assert_equal "text/plain", mail.parts[1].content_type
end
def test_html_mail
mail = TestMailer.create_html_mail(@recipient)
assert_equal "text/html", mail.content_type
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册