Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3959

Show
Ignore:
Timestamp:
03/18/06 23:53:07 (3 years ago)
Author:
minam
Message:

Parse content-type apart before using it so that sub-parts of the header can be set correctly (closes #2918)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionmailer/CHANGELOG

    r3956 r3959  
    11*SVN* 
     2 
     3* Parse content-type apart before using it so that sub-parts of the header can be set correctly #2918 [Jamis Buck] 
    24 
    35* Make custom headers work in subparts #4034 [elan@bluemandrill.com] 
  • trunk/actionmailer/lib/action_mailer/base.rb

    r3955 r3959  
    414414        headers.each { |k, v| m[k] = v } 
    415415 
     416        real_content_type, ctype_attrs = parse_content_type 
     417 
    416418        if @parts.empty? 
    417           m.set_content_type content_type, nil, { "charset" => charset } 
     419          m.set_content_type(real_content_type, nil, ctype_attrs) 
    418420          m.body = Utils.normalize_new_lines(body) 
    419421        else 
     
    421423            part = TMail::Mail.new 
    422424            part.body = Utils.normalize_new_lines(body) 
    423             part.set_content_type content_type, nil, { "charset" => charset } 
     425            part.set_content_type(real_content_type, nil, ctype_attrs) 
    424426            part.set_content_disposition "inline" 
    425427            m.parts << part 
     
    431433          end 
    432434           
    433           m.set_content_type(content_type, nil, { "charset" => charset }) if content_type =~ /multipart/ 
     435          m.set_content_type(real_content_type, nil, ctype_attrs) if real_content_type =~ /multipart/ 
    434436        end 
    435437 
  • trunk/actionmailer/lib/action_mailer/part_container.rb

    r2743 r3959  
    3939    end 
    4040 
     41    private 
     42     
     43      def parse_content_type(defaults=nil) 
     44        return [defaults && defaults.content_type, {}] if content_type.blank? 
     45        ctype, *attrs = content_type.split(/;\s*/) 
     46        attrs = attrs.inject({}) { |h,s| k,v = s.split(/=/, 2); h[k] = v; h } 
     47        [ctype, {"charset" => charset || defaults && defaults.charset}.merge(attrs)] 
     48      end 
     49 
    4150  end 
    4251end 
  • trunk/actionmailer/lib/action_mailer/part.rb

    r3956 r3959  
    5757      part = TMail::Mail.new 
    5858 
     59      real_content_type, ctype_attrs = parse_content_type(defaults) 
     60 
    5961      if @parts.empty? 
    6062        part.content_transfer_encoding = transfer_encoding || "quoted-printable" 
     
    7274        # non-attachment parts) 
    7375        if content_disposition == "attachment" 
    74           part.set_content_type(content_type || defaults.content_type, nil, 
    75             squish("charset" => nil, "name" => filename)) 
     76          ctype_attrs.delete "charset" 
     77          part.set_content_type(real_content_type, nil, 
     78            squish("name" => filename).merge(ctype_attrs)) 
    7679          part.set_content_disposition(content_disposition, 
    77             squish("filename" => filename)
     80            squish("filename" => filename).merge(ctype_attrs)
    7881        else 
    79           part.set_content_type(content_type || defaults.content_type, nil, 
    80             "charset" => (charset || defaults.charset))       
     82          part.set_content_type(real_content_type, nil, ctype_attrs) 
    8183          part.set_content_disposition(content_disposition)  
    8284        end         
     
    8587          part = TMail::Mail.new 
    8688          part.body = body 
    87           part.set_content_type content_type, nil, { "charset" => charset } 
     89          part.set_content_type(real_content_type, nil, ctype_attrs) 
    8890          part.set_content_disposition "inline" 
    8991          m.parts << part 
     
    9597        end 
    9698         
    97         part.set_content_type(content_type, nil, { "charset" => charset }) if content_type =~ /multipart/ 
     99        part.set_content_type(real_content_type, nil, ctype_attrs) if real_content_type =~ /multipart/ 
    98100      end 
    99101 
    100       @headers.each { |k,v| part[k] = v } 
     102      headers.each { |k,v| part[k] = v } 
    101103 
    102104      part 
     
    104106 
    105107    private 
     108 
    106109      def squish(values={}) 
    107110        values.delete_if { |k,v| v.nil? } 
  • trunk/actionmailer/test/mail_service_test.rb

    r3956 r3959  
    244244    cc           "Three: Four <test@example.com>" 
    245245    bcc          "Five: Six <test@example.com>" 
     246    body         "testing" 
     247  end 
     248 
     249  def custom_content_type_attributes 
     250    recipients   "no.one@nowhere.test" 
     251    subject      "custom content types" 
     252    from         "some.one@somewhere.test" 
     253    content_type "text/plain; format=flowed" 
    246254    body         "testing" 
    247255  end 
     
    788796    assert_equal 2, mail.parts.length 
    789797  end 
     798 
     799  def test_custom_content_type_attributes 
     800    mail = TestMailer.create_custom_content_type_attributes 
     801    assert_match %r{format=flowed}, mail['content-type'].to_s 
     802    assert_match %r{charset=utf-8}, mail['content-type'].to_s 
     803  end 
    790804end 
    791805