Skip to content
This repository
Browse code

Refactor content type setting, added tests to ensure boundary exists …

…on multipart and fixed typo
  • Loading branch information...
commit 74a5889abef1212d373ea994f1c93daedee8932c 1 parent 1b3cb54
José Valim and Mikel Lindsaar authored
31 actionmailer/lib/action_mailer/base.rb
@@ -49,7 +49,7 @@ module ActionMailer #:nodoc:
49 49 #
50 50 # The mail method, if not passed a block, will inspect your views and send all the views with
51 51 # the same name as the method, so the above action would send the +welcome.plain.erb+ view file
52   - # as well as the +welcome.html.erb+ view file in a +multipart/alternate+ email.
  52 + # as well as the +welcome.html.erb+ view file in a +multipart/alternative+ email.
53 53 #
54 54 # If you want to explicitly render only certain templates, pass a block:
55 55 #
@@ -162,7 +162,7 @@ module ActionMailer #:nodoc:
162 162 #
163 163 # Which will (if it had both a <tt>.text.erb</tt> and <tt>.html.erb</tt> tempalte in the view
164 164 # directory), send a complete <tt>multipart/mixed</tt> email with two parts, the first part being
165   - # a <tt>multipart/alternate</tt> with the text and HTML email parts inside, and the second being
  165 + # a <tt>multipart/alternative</tt> with the text and HTML email parts inside, and the second being
166 166 # a <tt>application/pdf</tt> with a Base64 encoded copy of the file.pdf book with the filename
167 167 # +free_book.pdf+.
168 168 #
@@ -445,7 +445,7 @@ def attachments
445 445 # format.html { render :text => "<h1>Hello Mikel!</h1>" }
446 446 # end
447 447 #
448   - # Which will render a <tt>multipart/alternate</tt> email with <tt>text/plain</tt> and
  448 + # Which will render a <tt>multipart/alternative</tt> email with <tt>text/plain</tt> and
449 449 # <tt>text/html</tt> parts.
450 450 def mail(headers={}, &block)
451 451 # Guard flag to prevent both the old and the new API from firing
@@ -465,10 +465,11 @@ def mail(headers={}, &block)
465 465
466 466 # Render the templates and blocks
467 467 responses, sort_order = collect_responses_and_sort_order(headers, &block)
468   - content_type ||= create_parts_from_responses(m, responses, charset)
  468 +
  469 + create_parts_from_responses(m, responses, charset)
469 470
470 471 # Tidy up content type, charset, mime version and sort order
471   - m.content_type = content_type
  472 + m.content_type = set_content_type(m, content_type)
472 473 m.charset = charset
473 474 m.mime_version = mime_version
474 475 sort_order = headers[:parts_order] || sort_order || self.class.default_implicit_parts_order.dup
@@ -485,6 +486,20 @@ def mail(headers={}, &block)
485 486
486 487 protected
487 488
  489 + def set_content_type(m, user_content_type)
  490 + params = m.content_type_parameters || {}
  491 + case
  492 + when user_content_type.present?
  493 + user_content_type
  494 + when m.has_attachments?
  495 + ["multipart", "mixed", params]
  496 + when m.multipart?
  497 + ["multipart", "alternative", params]
  498 + else
  499 + self.class.default_content_type.dup
  500 + end
  501 + end
  502 +
488 503 def default_subject #:nodoc:
489 504 mailer_scope = self.class.mailer_name.gsub('/', '.')
490 505 I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize)
@@ -543,16 +558,14 @@ def create_parts_from_responses(m, responses, charset) #:nodoc:
543 558 if responses.size == 1 && !m.has_attachments?
544 559 m.body = responses[0][:body]
545 560 return responses[0][:content_type]
546   - elsif responses.size > 1 && m.has_attachments?
  561 + elsif responses.size > 1 && m.has_attachments?
547 562 container = Mail::Part.new
548   - container.content_type = "multipart/alternate"
  563 + container.content_type = "multipart/alternative"
549 564 responses.each { |r| insert_part(container, r, charset) }
550 565 m.add_part(container)
551 566 else
552 567 responses.each { |r| insert_part(m, r, charset) }
553 568 end
554   -
555   - m.has_attachments? ? "multipart/mixed" : "multipart/alternate"
556 569 end
557 570
558 571 def insert_part(container, response, charset) #:nodoc:
23 actionmailer/test/base_test.rb
@@ -200,7 +200,7 @@ def explicit_multipart_with_any(hash = {})
200 200 test "implicit multipart" do
201 201 email = BaseMailer.implicit_multipart.deliver
202 202 assert_equal(2, email.parts.size)
203   - assert_equal("multipart/alternate", email.mime_type)
  203 + assert_equal("multipart/alternative", email.mime_type)
204 204 assert_equal("text/plain", email.parts[0].mime_type)
205 205 assert_equal("TEXT Implicit Multipart", email.parts[0].body.encoded)
206 206 assert_equal("text/html", email.parts[1].mime_type)
@@ -223,7 +223,7 @@ def explicit_multipart_with_any(hash = {})
223 223 test "implicit multipart with attachments creates nested parts" do
224 224 email = BaseMailer.implicit_multipart(:attachments => true).deliver
225 225 assert_equal("application/pdf", email.parts[0].mime_type)
226   - assert_equal("multipart/alternate", email.parts[1].mime_type)
  226 + assert_equal("multipart/alternative", email.parts[1].mime_type)
227 227 assert_equal("text/plain", email.parts[1].parts[0].mime_type)
228 228 assert_equal("TEXT Implicit Multipart", email.parts[1].parts[0].body.encoded)
229 229 assert_equal("text/html", email.parts[1].parts[1].mime_type)
@@ -235,7 +235,7 @@ def explicit_multipart_with_any(hash = {})
235 235 swap BaseMailer, :default_implicit_parts_order => order do
236 236 email = BaseMailer.implicit_multipart(:attachments => true).deliver
237 237 assert_equal("application/pdf", email.parts[0].mime_type)
238   - assert_equal("multipart/alternate", email.parts[1].mime_type)
  238 + assert_equal("multipart/alternative", email.parts[1].mime_type)
239 239 assert_equal("text/plain", email.parts[1].parts[1].mime_type)
240 240 assert_equal("text/html", email.parts[1].parts[0].mime_type)
241 241 end
@@ -244,7 +244,7 @@ def explicit_multipart_with_any(hash = {})
244 244 test "implicit multipart with default locale" do
245 245 email = BaseMailer.implicit_with_locale.deliver
246 246 assert_equal(2, email.parts.size)
247   - assert_equal("multipart/alternate", email.mime_type)
  247 + assert_equal("multipart/alternative", email.mime_type)
248 248 assert_equal("text/plain", email.parts[0].mime_type)
249 249 assert_equal("Implicit with locale TEXT", email.parts[0].body.encoded)
250 250 assert_equal("text/html", email.parts[1].mime_type)
@@ -255,7 +255,7 @@ def explicit_multipart_with_any(hash = {})
255 255 swap I18n, :locale => :pl do
256 256 email = BaseMailer.implicit_with_locale.deliver
257 257 assert_equal(2, email.parts.size)
258   - assert_equal("multipart/alternate", email.mime_type)
  258 + assert_equal("multipart/alternative", email.mime_type)
259 259 assert_equal("text/plain", email.parts[0].mime_type)
260 260 assert_equal("Implicit with locale PL TEXT", email.parts[0].body.encoded)
261 261 assert_equal("text/html", email.parts[1].mime_type)
@@ -287,7 +287,7 @@ def explicit_multipart_with_any(hash = {})
287 287 test "explicit multipart" do
288 288 email = BaseMailer.explicit_multipart.deliver
289 289 assert_equal(2, email.parts.size)
290   - assert_equal("multipart/alternate", email.mime_type)
  290 + assert_equal("multipart/alternative", email.mime_type)
291 291 assert_equal("text/plain", email.parts[0].mime_type)
292 292 assert_equal("TEXT Explicit Multipart", email.parts[0].body.encoded)
293 293 assert_equal("text/html", email.parts[1].mime_type)
@@ -310,7 +310,7 @@ def explicit_multipart_with_any(hash = {})
310 310 test "explicit multipart with attachments creates nested parts" do
311 311 email = BaseMailer.explicit_multipart(:attachments => true).deliver
312 312 assert_equal("application/pdf", email.parts[0].mime_type)
313   - assert_equal("multipart/alternate", email.parts[1].mime_type)
  313 + assert_equal("multipart/alternative", email.parts[1].mime_type)
314 314 assert_equal("text/plain", email.parts[1].parts[0].mime_type)
315 315 assert_equal("TEXT Explicit Multipart", email.parts[1].parts[0].body.encoded)
316 316 assert_equal("text/html", email.parts[1].parts[1].mime_type)
@@ -320,7 +320,7 @@ def explicit_multipart_with_any(hash = {})
320 320 test "explicit multipart with templates" do
321 321 email = BaseMailer.explicit_multipart_templates.deliver
322 322 assert_equal(2, email.parts.size)
323   - assert_equal("multipart/alternate", email.mime_type)
  323 + assert_equal("multipart/alternative", email.mime_type)
324 324 assert_equal("text/html", email.parts[0].mime_type)
325 325 assert_equal("HTML Explicit Multipart Templates", email.parts[0].body.encoded)
326 326 assert_equal("text/plain", email.parts[1].mime_type)
@@ -330,7 +330,7 @@ def explicit_multipart_with_any(hash = {})
330 330 test "explicit multipart with any" do
331 331 email = BaseMailer.explicit_multipart_with_any.deliver
332 332 assert_equal(2, email.parts.size)
333   - assert_equal("multipart/alternate", email.mime_type)
  333 + assert_equal("multipart/alternative", email.mime_type)
334 334 assert_equal("text/plain", email.parts[0].mime_type)
335 335 assert_equal("Format with any!", email.parts[0].body.encoded)
336 336 assert_equal("text/html", email.parts[1].mime_type)
@@ -370,6 +370,11 @@ def explicit_multipart_with_any(hash = {})
370 370 BaseMailer.expects(:welcome).returns(mail)
371 371 BaseMailer.welcome.deliver
372 372 end
  373 +
  374 + test "explicit multipart should be multipart" do
  375 + mail = BaseMailer.explicit_multipart
  376 + assert_not_nil(mail.content_type_parameters[:boundary])
  377 + end
373 378
374 379 protected
375 380

0 comments on commit 74a5889

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