1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

More updates... 45 errors left to get it working with Mail gem

This commit is contained in:
Mikel Lindsaar 2009-11-20 14:10:57 +11:00
parent 15d7cac282
commit 539d9b355f
9 changed files with 94 additions and 72 deletions

View file

@ -366,10 +366,27 @@ module ActionMailer #:nodoc:
# Alias controller_path to mailer_name so render :partial in views work.
alias :controller_path :mailer_name
# Add a part to a multipart message, with the given content-type. The
# part itself is yielded to the block so that other properties (charset,
# body, headers, etc.) can be set on it.
def part(params)
params = {:content_type => params} if String === params
if custom_headers = params.delete(:headers)
STDERR.puts("Passing custom headers with :headers => {} is deprecated. Please just pass in custom headers directly.")
params = params.merge(custom_headers)
end
part = Mail::Part.new(params)
yield part
self.parts << part
yield part if block_given?
@parts << part
end
# Add an attachment to a multipart message. This is simply a part with the
# content-disposition set to "attachment".
def attachment(params, &block)
params = { :content_type => params } if String === params
params = { :disposition => "attachment",
:transfer_encoding => "base64" }.merge(params)
part(params, &block)
end
class << self
@ -417,8 +434,7 @@ module ActionMailer #:nodoc:
# end
def receive(raw_email)
logger.info "Received mail:\n #{raw_email}" unless logger.nil?
mail = Mail.parse(raw_email)
mail.base64_decode
mail = Mail.new(raw_email)
new.receive(mail)
end
@ -598,26 +614,32 @@ module ActionMailer #:nodoc:
real_content_type, ctype_attrs = parse_content_type
if @parts.empty?
m.set_content_type(real_content_type, nil, ctype_attrs)
main_type, sub_type = split_content_type(real_content_type)
m.content_type(main_type, sub_type, ctype_attrs)
m.body = normalize_new_lines(body)
elsif @parts.size == 1 && @parts.first.parts.empty?
m.set_content_type(real_content_type, nil, ctype_attrs)
main_type, sub_type = split_content_type(real_content_type)
m.content_type(main_type, sub_type, ctype_attrs)
m.body = normalize_new_lines(@parts.first.body)
else
@parts.each do |p|
part = (Mail === p ? p : p.to_mail(self))
m.parts << part
m.parts << p
end
if real_content_type =~ /multipart/
ctype_attrs.delete "charset"
m.set_content_type(real_content_type, nil, ctype_attrs)
main_type, sub_type = split_content_type(real_content_type)
m.content_type([main_type.to_s, sub_type.to_s, ctype_attrs])
end
end
@mail = m
end
def split_content_type(ct)
ct.to_s.split("/")
end
def parse_content_type(defaults=nil)
if content_type.blank?
return defaults ?

View file

@ -44,7 +44,7 @@ module ActionMailer
def set_expected_mail
@expected = Mail.new
@expected.set_content_type "text", "plain", { "charset" => charset }
@expected.content_type ["text", "plain", { "charset" => charset }]
@expected.mime_version = '1.0'
end

View file

@ -2,22 +2,8 @@
# Created in 1.2 of Mail. Will be deprecated
STDERR.puts("DEPRECATION WARNING, Mail running in TMail compatibility mode. This will be deprecated soon.")
module Mail
def Mail.parse(string)
STDERR.puts("DEPRECATION WARNING, Mail.parse(string) is deprecated, please use Mail.new")
::Mail.new(string)
end
end
class Mail::Message
def set_content_type(*args)
STDERR.puts("DEPRECATION WARNING, Message#set_content_type is deprecated, please use Message#content_type")
content_type(args)
end
def set_content_disposition(*args)
STDERR.puts("DEPRECATION WARNING, Message#set_content_disposition is deprecated, please use Message#content_disposition")
content_disposition(args)

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

View file

@ -56,7 +56,7 @@ end
class MailerHelperTest < Test::Unit::TestCase
def new_mail( charset="utf-8" )
mail = Mail.new
mail.set_content_type "text", "plain", { "charset" => charset } if charset
mail.content_type ["text", "plain", { "charset" => charset }]
mail
end

View file

@ -252,7 +252,7 @@ class TestMailer < ActionMailer::Base
from "test@example.com"
content_type "multipart/related"
part :content_type => "text/html", :body => 'yo'
attachment :content_type => "image/jpeg",:filename => "test.jpeg", :body => "i am not a real picture", :headers => { 'Content-ID' => '<test@test.com>' }
attachment :content_type => "image/jpeg", :filename => File.join(File.dirname(__FILE__), "fixtures", "attachments", "test.jpg"), :body => "i am not a real picture", :headers => { 'Content-ID' => '<test@test.com>' }
end
def unnamed_attachment(recipient)
@ -272,11 +272,12 @@ class TestMailer < ActionMailer::Base
bcc "Five: Six <test@example.com>"
render :text => "testing"
end
require 'ruby-debug'
def custom_content_type_attributes
recipients "no.one@nowhere.test"
subject "custom content types"
from "some.one@somewhere.test"
debugger
content_type "text/plain; format=flowed"
render :text => "testing"
end
@ -322,7 +323,7 @@ class ActionMailerTest < Test::Unit::TestCase
mail = Mail.new
mail.mime_version = "1.0"
if charset
mail.set_content_type "text", "plain", { "charset" => charset }
mail.content_type ["text", "plain", { "charset" => charset }]
end
mail
end
@ -375,6 +376,7 @@ class ActionMailerTest < Test::Unit::TestCase
def test_attachment_with_custom_header
created = nil
assert_nothing_raised { created = TestMailer.create_attachment_with_custom_header(@recipient) }
created.encoded
assert created.parts.any? { |p| p.header['content-id'].to_s == "<test@test.com>" }
end
@ -440,12 +442,12 @@ class ActionMailerTest < Test::Unit::TestCase
assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
assert_not_nil created
assert_equal 2, created.parts.length
assert_equal 'text/plain', created.parts[0].content_type
assert_equal 'text/html', created.parts[1].content_type
assert_equal 'text/plain', created.parts[0].content_type.content_type
assert_equal 'text/html', created.parts[1].content_type.content_type
end
def test_cancelled_account
expected = new_mail
expected = new_mail('US-ASCII')
expected.to = @recipient
expected.subject = "[Cancelled] Goodbye #{@recipient}"
expected.body = "Goodbye, Mr. #{@recipient}"
@ -455,15 +457,21 @@ class ActionMailerTest < Test::Unit::TestCase
created = nil
assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) }
assert_not_nil created
expected.message_id = '<123@456>'
created.message_id = '<123@456>'
assert_equal expected.encoded, created.encoded
assert_nothing_raised { TestMailer.deliver_cancelled_account(@recipient) }
assert_not_nil ActionMailer::Base.deliveries.first
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
delivered = ActionMailer::Base.deliveries.first
expected.message_id = '<123@456>'
delivered.message_id = '<123@456>'
assert_equal expected.encoded, delivered.encoded
end
def test_cc_bcc
expected = new_mail
expected = new_mail('US-ASCII')
expected.to = @recipient
expected.subject = "testing bcc/cc"
expected.body = "Nothing to see here."
@ -477,6 +485,8 @@ class ActionMailerTest < Test::Unit::TestCase
created = TestMailer.create_cc_bcc @recipient
end
assert_not_nil created
expected.message_id = '<123@456>'
created.message_id = '<123@456>'
assert_equal expected.encoded, created.encoded
assert_nothing_raised do
@ -484,7 +494,11 @@ class ActionMailerTest < Test::Unit::TestCase
end
assert_not_nil ActionMailer::Base.deliveries.first
assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
delivered = ActionMailer::Base.deliveries.first
expected.message_id = '<123@456>'
delivered.message_id = '<123@456>'
assert_equal expected.encoded, delivered.encoded
end
def test_reply_to
@ -636,7 +650,7 @@ Content-Type: text/plain; charset=iso-8859-1
The body
EOF
mail = Mail.parse(msg)
mail = Mail.new(msg)
assert_equal "testing testing \326\244", mail.subject.decoded
assert_equal "=?utf-8?Q?testing_testing_=D6=A4?=", mail.quoted_subject
end
@ -649,7 +663,7 @@ Content-Type: text/plain; charset=iso-8859-1
The body
EOF
mail = Mail.parse(msg)
mail = Mail.new(msg)
assert_equal "this == working?", mail.subject.decoded
assert_equal "this == working?", mail.quoted_subject
end
@ -663,7 +677,7 @@ Content-Transfer-Encoding: 7bit
The=3Dbody
EOF
mail = Mail.parse(msg)
mail = Mail.new(msg)
assert_equal "The=3Dbody", mail.body.decoded.strip
assert_equal "The=3Dbody", mail.quoted_body.strip
end
@ -677,7 +691,7 @@ Content-Transfer-Encoding: quoted-printable
The=3Dbody
EOF
mail = Mail.parse(msg)
mail = Mail.new(msg)
assert_equal "The=body", mail.body.decoded.strip
assert_equal "The=3Dbody", mail.quoted_body.strip
end
@ -691,7 +705,7 @@ Content-Transfer-Encoding: base64
VGhlIGJvZHk=
EOF
mail = Mail.parse(msg)
mail = Mail.new(msg)
assert_equal "The body", mail.body.decoded.strip
assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip
end
@ -763,7 +777,7 @@ EOF
def test_receive_attachments
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email2")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
attachment = mail.attachments.last
assert_equal "smime.p7s", attachment.original_filename
assert_equal "application/pkcs7-signature", attachment.content_type
@ -771,21 +785,21 @@ EOF
def test_decode_attachment_without_charset
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email3")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
attachment = mail.attachments.last
assert_equal 1026, attachment.read.length
end
def test_attachment_using_content_location
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email12")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
assert_equal 1, mail.attachments.length
assert_equal "Photo25.jpg", mail.attachments.first.original_filename
end
def test_attachment_with_text_type
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email13")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
assert mail.has_attachments?
assert_equal 1, mail.attachments.length
assert_equal "hello.rb", mail.attachments.first.original_filename
@ -793,19 +807,19 @@ EOF
def test_decode_part_without_content_type
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email4")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
assert_nothing_raised { mail.body }
end
def test_decode_message_without_content_type
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email5")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
assert_nothing_raised { mail.body }
end
def test_decode_message_with_incorrect_charset
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email6")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
assert_nothing_raised { mail.body }
end
@ -858,14 +872,14 @@ EOF
mail = TestMailer.create_implicitly_multipart_example(@recipient)
assert_equal 3, mail.parts.length
assert_equal "1.0", mail.mime_version
assert_equal "multipart/alternative", mail.content_type
assert_equal "application/x-yaml", mail.parts[0].content_type
assert_equal "utf-8", mail.parts[0].sub_header("content-type", "charset")
assert_equal "text/plain", mail.parts[1].content_type
assert_equal "utf-8", mail.parts[1].sub_header("content-type", "charset")
assert_equal "text/html", mail.parts[2].content_type
assert_equal "utf-8", mail.parts[2].sub_header("content-type", "charset")
assert_equal "1.0", mail.mime_version.decoded
assert_equal "multipart/alternative", mail.content_type.content_type
assert_equal "application/x-yaml", mail.parts[0].content_type.content_type
assert_equal "utf-8", mail.parts[0].content_type.parameters[:charset]
assert_equal "text/plain", mail.parts[1].content_type.content_type
assert_equal "utf-8", mail.parts[1].content_type.parameters[:charset]
assert_equal "text/html", mail.parts[2].content_type.content_type
assert_equal "utf-8", mail.parts[2].content_type.parameters[:charset]
end
def test_implicitly_multipart_messages_with_custom_order
@ -873,19 +887,19 @@ EOF
mail = TestMailer.create_implicitly_multipart_example(@recipient, nil, ["application/x-yaml", "text/plain"])
assert_equal 3, mail.parts.length
assert_equal "text/html", mail.parts[0].content_type
assert_equal "text/plain", mail.parts[1].content_type
assert_equal "application/x-yaml", mail.parts[2].content_type
assert_equal "text/html", mail.parts[0].content_type.content_type
assert_equal "text/plain", mail.parts[1].content_type.content_type
assert_equal "application/x-yaml", mail.parts[2].content_type.content_type
end
def test_implicitly_multipart_messages_with_charset
mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
assert_equal "multipart/alternative", mail.header['content-type'].body
assert_equal "multipart/alternative", mail.header['content-type'].content_type
assert_equal 'iso-8859-1', mail.parts[0].sub_header("content-type", "charset")
assert_equal 'iso-8859-1', mail.parts[1].sub_header("content-type", "charset")
assert_equal 'iso-8859-1', mail.parts[2].sub_header("content-type", "charset")
assert_equal 'iso-8859-1', mail.parts[0].content_type.parameters[:charset]
assert_equal 'iso-8859-1', mail.parts[1].content_type.parameters[:charset]
assert_equal 'iso-8859-1', mail.parts[2].content_type.parameters[:charset]
end
def test_html_mail
@ -935,13 +949,13 @@ EOF
def test_recursive_multipart_processing
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
assert_equal "This is the first part.\n\nAttachment: test.rb\nAttachment: test.pdf\n\n\nAttachment: smime.p7s\n", mail.body.decoded
end
def test_decode_encoded_attachment_filename
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
attachment = mail.attachments.last
expected = "01 Quien Te Dij\212at. Pitbull.mp3"
@ -952,7 +966,7 @@ EOF
def test_decode_message_with_unknown_charset
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
assert_nothing_raised { mail.body }
end
@ -987,8 +1001,8 @@ EOF
def test_custom_content_type_attributes
mail = TestMailer.create_custom_content_type_attributes
assert_match %r{format=flowed}, mail['content-type'].to_s
assert_match %r{charset=utf-8}, mail['content-type'].to_s
assert_match %r{format=flowed}, mail.content_type.encoded
assert_match %r{charset=utf-8}, mail.content_type.encoded
end
def test_return_path_with_create
@ -1005,7 +1019,7 @@ EOF
def test_body_is_stored_as_an_ivar
mail = TestMailer.create_body_ivar(@recipient)
assert_equal "body: foo\nbar: baz", mail.body
assert_equal "body: foo\nbar: baz", mail.body.encoded
end
def test_starttls_is_enabled_if_supported

View file

@ -65,14 +65,14 @@ class QuotingTest < Test::Unit::TestCase
# test an email that has been created using \r\n newlines, instead of
# \n newlines.
def test_email_quoted_with_0d0a
mail = Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_quoted_with_0d0a"))
mail = Mail.new(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_quoted_with_0d0a"))
# CHANGED: subject returns an object now
# assert_match %r{Elapsed time}, mail.body
assert_match %r{Elapsed time}, mail.body.decoded
end
def test_email_with_partially_quoted_subject
mail = Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_partially_quoted_subject"))
mail = Mail.new(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_partially_quoted_subject"))
# CHANGED: subject returns an object now
# assert_equal "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail", mail.subject
assert_equal "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail", mail.subject.decoded

View file

@ -14,7 +14,7 @@ class TMailMailTest < Test::Unit::TestCase
def test_nested_attachments_are_recognized_correctly
fixture = File.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_nested_attachment")
mail = Mail.parse(fixture)
mail = Mail.new(fixture)
assert_equal 2, mail.attachments.length
assert_equal "image/png", mail.attachments.first.content_type
assert_equal 1902, mail.attachments.first.length

View file

@ -34,7 +34,7 @@ class ActionMailerUrlTest < Test::Unit::TestCase
mail = Mail.new
mail.mime_version = "1.0"
if charset
mail.set_content_type "text", "plain", { "charset" => charset }
mail.content_type ["text", "plain", { "charset" => charset }]
end
mail
end