Sha256: 0af84ce0de6fa72a03c13091c86311b03c877db4cd211def5924b3b57f355b71

Contents?: true

Size: 1.27 KB

Versions: 16

Compression:

Stored size: 1.27 KB

Contents

=begin rdoc

= Attachment handling class

=end

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

16 entries across 16 versions & 4 rubygems

Version Path
actionmailer-2.0.2 lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
actionmailer-2.0.4 lib/action_mailer/vendor/tmail-1.2.1/tmail/attachments.rb
actionmailer-2.0.0 lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
actionmailer-2.0.1 lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
actionmailer-2.0.5 lib/action_mailer/vendor/tmail-1.2.1/tmail/attachments.rb
radiant-0.6.5.1 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
radiant-0.6.5 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
radiant-0.6.7 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
radiant-0.6.6 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
radiant-0.6.9 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
radiant-0.6.8 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
spree-0.0.9 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
spree-0.2.0 vendor/rails/actionmailer/lib/action_mailer/vendor/tmail-1.1.0/tmail/attachments.rb
tmail-1.2.0 lib/tmail/attachments.rb
tmail-1.1.1 lib/tmail/attachments.rb
tmail-1.2.1 lib/tmail/attachments.rb