Sha256: beac2c078a29eba5962cfca9439e6ad6458a90b2d4119c6a50299284dfcbcb27

Contents?: true

Size: 1.22 KB

Versions: 56

Compression:

Stored size: 1.22 KB

Contents

require 'stringio'

module TMail
  class Attachment < StringIO
    attr_accessor :original_filename, :content_type
  end

  class Mail
    def has_attachments?
      multipart? && parts.any? { |part| attachment?(part) }
    end

    def attachment?(part)
      (part['content-disposition'] && part['content-disposition'].disposition == "attachment") ||
      part.header['content-type'].main_type != "text"
    end

    def attachments
      if multipart?
        parts.collect { |part| 
          if part.multipart?
            part.attachments
          elsif attachment?(part)
            content   = part.body # unquoted automatically by TMail#body
            file_name = (part['content-location'] &&
                          part['content-location'].body) ||
                        part.sub_header("content-type", "name") ||
                        part.sub_header("content-disposition", "filename")
            
            next if file_name.blank? || content.blank?
            
            attachment = Attachment.new(content)
            attachment.original_filename = file_name.strip
            attachment.content_type = part.content_type
            attachment
          end
        }.flatten.compact
      end      
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
backlog-0.0.0 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.0.1 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.0.2 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.0.5 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.0.4 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.1.0 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.1.1 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.1.2 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.2.0 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.2.1 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.0 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.2 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.1 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.3 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.4 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.6 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.5 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.7 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.8 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb
backlog-0.3.9 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail/attachments.rb