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

Ticket #8954 (new defect)

Opened 2 years ago

Last modified 1 year ago

[PATCH] Ability to set the filename for inline MIME parts.

Reported by: vjebelev Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionMailer Version: edge
Severity: normal Keywords: actionmailer inline filename
Cc:

Description

According to RFC2183 any Content-Disposition: header may contain the filename parameter: for attachments as well as inline disposition types:

     disposition := "Content-Disposition" ":"
                    disposition-type
                    *(";" disposition-parm)

     disposition-type := "inline"
                       / "attachment"
                       / extension-token
                       ; values are not case-sensitive

     disposition-parm := filename-parm
                       / creation-date-parm
                       / modification-date-parm
                       / read-date-parm
                       / size-parm
                       / parameter

     filename-parm := "filename" "=" value

Setting the filename on an inline MIME part is useful in situations when it's desirable to include some files as inline parts (e.g. inline image, pdf etc): client MUA or webmail then presents the correct filename when the user wants to download the file. Currently, ActionMailer adds the filename parameter for attachments only (part.rb):

        if content_disposition == "attachment"
          ctype_attrs.delete "charset"
          part.set_content_type(real_content_type, nil,
            squish("name" => filename).merge(ctype_attrs))
          part.set_content_disposition(content_disposition,
            squish("filename" => filename).merge(ctype_attrs))
        else
          part.set_content_type(real_content_type, nil, ctype_attrs)
          part.set_content_disposition(content_disposition)
        end

I suggest to replace the above code with the following:

        if content_disposition == "attachment"
          ctype_attrs.delete "charset"
        else
          part.set_content_type(real_content_type, nil, ctype_attrs)
        end
        part.set_content_type(real_content_type, nil,
          squish("name" => filename).merge(ctype_attrs))
        part.set_content_disposition(content_disposition,
          squish("filename" => filename).merge(ctype_attrs))

When the name or filename parameters are not used by the caller, the Content-Type: and Content-Disposition: headers will not be affected. For consistency, the name parameter is also added to the Content-Type: header when filename is provided ( see RFC2045 for discussion about media types and parameters).

Code patch to the edge rails and units are attached. With the patch applied, it's easy to include inline images with correct filenames, e.g.:

    part :content_type => 'text/plain',
         :body => 'Hello text!'

    part 'multipart/mixed' do |p|
      p.content_type = 'text/html'

      p.part :content_type => 'image/png',
             :content_disposition => 'inline',
             :filename => 'rails-logo.png',
             :transfer_encoding => 'base64',
             :body => File.read('/tmp/rails.png')

      p.part :content_type => 'text/html',
             :body => '<html><body>inline image with filename provided</body></html>'
    end

Attachments

add_filename_to_inline_mime_parts.diff (6.4 kB) - added by vjebelev on 07/12/07 14:47:52.

Change History

07/12/07 14:47:52 changed by vjebelev

  • attachment add_filename_to_inline_mime_parts.diff added.

02/07/08 07:42:15 changed by sameer

+1

the front-end (via send_data) allows you to specify a filename for inline content. there's no reason why ActionMailer shouldn't support this for inline email attachments.