Skip to content
Browse files

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

  • Loading branch information...
1 parent 15d7cac commit 539d9b355f40b1668b4ad4e8fe628e0a3d9760b8 @mikel mikel committed Nov 20, 2009
View
40 actionmailer/lib/action_mailer/base.rb
@@ -366,10 +366,27 @@ def mailer_name(value = nil)
# 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 @@ def method_missing(method_symbol, *parameters) #: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,25 +614,31 @@ def create_mail
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?
View
2 actionmailer/lib/action_mailer/test_case.rb
@@ -44,7 +44,7 @@ def initialize_test_deliveries
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
14 actionmailer/lib/action_mailer/vendor/tmail_compat.rb
@@ -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)
View
BIN actionmailer/test/fixtures/attachments/test.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
2 actionmailer/test/mail_helper_test.rb
@@ -56,7 +56,7 @@ def name_of_the_mailer_class
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
100 actionmailer/test/mail_service_test.rb
@@ -251,8 +251,8 @@ def attachment_with_custom_header(recipient)
subject "custom header in attachment"
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>' }
+ part :content_type => "text/html", :body => 'yo'
+ 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 @@ def headers_with_nonalpha_chars(recipient)
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 @@ def new_mail( charset="utf-8" )
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 @@ def test_nested_parts_with_body
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 @@ def test_custom_templating_extension
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 @@ def test_cancelled_account
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,14 +485,20 @@ def test_cc_bcc
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
TestMailer.deliver_cc_bcc @recipient
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 @@ def test_unquote_quoted_printable_subject
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 @@ def test_unquote_7bit_subject
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 @@ def test_unquote_7bit_body
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 @@ def test_unquote_quoted_printable_body
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 @@ def test_unquote_base64_body
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,49 +777,49 @@ def test_receive_decodes_base64_encoded_mail
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
end
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
end
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,34 +872,34 @@ def test_implicitly_multipart_messages
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
assert ActionView::Template.template_handler_extensions.include?("bak"), "bak extension was not registered"
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 @@ def test_file_delivery_should_create_a_file
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 @@ def test_decode_encoded_attachment_filename
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 @@ def test_multipart_with_template_path_with_dots
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 @@ def test_return_path_with_deliver
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
4 actionmailer/test/quoting_test.rb
@@ -65,14 +65,14 @@ def test_quote_multibyte_chars
# 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
2 actionmailer/test/tmail_test.rb
@@ -14,7 +14,7 @@ def test_body
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
2 actionmailer/test/url_test.rb
@@ -34,7 +34,7 @@ def new_mail( charset="utf-8" )
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

0 comments on commit 539d9b3

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