Class: Mail::AttachmentsList

### Home Classes Methods

### In Files

- lib/mail/attachments_list.rb

### Parent

Array

### Methods

- ::new
- #[]
- #[]=
- #guess_encoding
- #inline
- #set_mime_type

### Files

- TODO.rdoc

### Class/Module Index

Quicksearch

- Mail
- Mail::Address
- Mail::AddressContainer
- Mail::AttachmentsList
- Mail::BccField
- Mail::Body
- Mail::CcField
- Mail::CheckDeliveryParams
- Mail::CommentsField
- Mail::Constants
- Mail::ContentDescriptionField
- Mail::ContentDispositionField
- Mail::ContentIdField
- Mail::ContentLocationField
- Mail::ContentTransferEncodingElement
- Mail::ContentTransferEncodingField
- Mail::ContentTypeField
- Mail::DateField
- Mail::Encodings
- Mail::Encodings::Base64
- Mail::Encodings::Binary
- Mail::Encodings::EightBit
- Mail::Encodings::QuotedPrintable
- Mail::Encodings::SevenBit
- Mail::Encodings::TransferEncoding
- Mail::Encodings::UnixToUnix
- Mail::Envelope
- Mail::EnvelopeFromElement
- Mail::Exim
- Mail::Field
- Mail::Field::FieldError
- Mail::FieldList
- Mail::FileDelivery
- Mail::FromField
- Mail::Header
- Mail::IMAP
- Mail::InReplyToField
- Mail::IndifferentHash
- Mail::KeywordsField
- Mail::Matchers
- Mail::Matchers::HasSentEmailMatcher
- Mail::MessageIdField
- Mail::MessageIdsElement
- Mail::MimeVersionElement
- Mail::MimeVersionField
- Mail::Multibyte
- Mail::Multibyte::EncodingError
- Mail::OptionalField
- Mail::POP3
- Mail::ParameterHash
- Mail::Parsers
- Mail::Parsers::AddressListsParser
- Mail::Parsers::ContentDispositionParser
- Mail::Parsers::ContentLocationParser
- Mail::Parsers::ContentTransferEncodingParser
- Mail::Parsers::ContentTypeParser
- Mail::Parsers::DateTimeParser
- Mail::Parsers::EnvelopeFromParser
- Mail::Parsers::MessageIdsParser
- Mail::Parsers::MimeVersionParser
- Mail::Parsers::PhraseListsParser
- Mail::Parsers::Ragel
- Mail::Parsers::Ragel::AddressListsMachine
- Mail::Parsers::Ragel::ContentDispositionMachine
- Mail::Parsers::Ragel::ContentLocationMachine
- Mail::Parsers::Ragel::ContentTransferEncodingMachine
- Mail::Parsers::Ragel::ContentTypeMachine
- Mail::Parsers::Ragel::DateTimeMachine
- Mail::Parsers::Ragel::EnvelopeFromMachine
- Mail::Parsers::Ragel::MessageIdsMachine
- Mail::Parsers::Ragel::MimeVersionMachine
- Mail::Parsers::Ragel::PhraseListsMachine
- Mail::Parsers::Ragel::ReceivedMachine
- Mail::Parsers::Ragel::Ruby
- Mail::Parsers::ReceivedParser
- Mail::Part
- Mail::PartsList
- Mail::PhraseList
- Mail::ReceivedElement
- Mail::ReceivedField
- Mail::ReferencesField
- Mail::ReplyToField
- Mail::ResentBccField
- Mail::ResentCcField
- Mail::ResentDateField
- Mail::ResentFromField
- Mail::ResentMessageIdField
- Mail::ResentSenderField
- Mail::ResentToField
- Mail::Retriever
- Mail::ReturnPathField
- Mail::Ruby18
- Mail::Ruby19
- Mail::Ruby19::BestEffortCharsetEncoder
- Mail::Ruby19::StrictCharsetEncoder
- Mail::SMTP
- Mail::SMTPConnection
- Mail::SenderField
- Mail::Sendmail
- Mail::StructuredField
- Mail::SubjectField
- Mail::TestMailer
- Mail::TestRetriever
- Mail::ToField
- Mail::UnstructuredField
- Mail::Utilities
- Mail::VERSION
- Net
- Net::SMTP
- Object
- String

No matching classes.

# Mail::AttachmentsList

### Public Class Methods

new(parts_list) click to toggle source

    # File lib/mail/attachments_list.rb, line 4
    def initialize(parts_list)
      @parts_list = parts_list
      @content_disposition_type = 'attachment'
      parts_list.map { |p|
        if p.content_type == "message/rfc822"
          Mail.new(p.body).attachments
        elsif p.parts.empty?
          p if p.attachment?
        else
          p.attachments
        end
      }.flatten.compact.each { |a| self << a }
      self
    end

### Public Instance Methods

[](index_value) click to toggle source

Returns the attachment by filename or at index.

mail.attachments = File.read('test.png') mail.attachments = File.read('test.jpg')

mail.attachments.filename #=> 'test.png' mail.attachments.filename #=> 'test.jpg'

    # File lib/mail/attachments_list.rb, line 31
    def [](index_value)
      if index_value.is_a?(Fixnum)
        self.fetch(index_value)
      else
        self.select { |a| a.filename == index_value }.first
      end
    end

[]=(name, value) click to toggle source

    # File lib/mail/attachments_list.rb, line 39
    def []=(name, value)
      encoded_name = Mail::Encodings.decode_encode name, :encode
      default_values = { :content_type => "#{set_mime_type(name)}; filename=\"#{encoded_name}\"",
                         :content_transfer_encoding => "#{guess_encoding}",
                         :content_disposition => "#{@content_disposition_type}; filename=\"#{encoded_name}\"" }

      if value.is_a?(Hash)

        default_values[:body] = value.delete(:content) if value[:content]

        default_values[:body] = value.delete(:data) if value[:data]

        encoding = value.delete(:transfer_encoding) || value.delete(:encoding)
        if encoding
          if Mail::Encodings.defined? encoding
            default_values[:content_transfer_encoding] = encoding
          else
            raise "Do not know how to handle Content Transfer Encoding #{encoding}, please choose either quoted-printable or base64"
          end
        end

        if value[:mime_type]
          default_values[:content_type] = value.delete(:mime_type)
          @mime_type = MIME::Types[default_values[:content_type]].first
          default_values[:content_transfer_encoding] ||= guess_encoding
        end

        hash = default_values.merge(value)
      else
        default_values[:body] = value
        hash = default_values
      end

      if hash[:body].respond_to? :force_encoding and hash[:body].respond_to? :valid_encoding?
        if not hash[:body].valid_encoding? and default_values[:content_transfer_encoding].downcase == "binary"
          hash[:body].force_encoding("BINARY")
        end
      end

      attachment = Part.new(hash)
      attachment.add_content_id(hash[:content_id])

      @parts_list << attachment
    end

guess_encoding() click to toggle source

Uses the mime type to try and guess the encoding, if it is a binary type, or unknown, then we set it to binary, otherwise as set to plain text

    # File lib/mail/attachments_list.rb, line 86
    def guess_encoding
      if @mime_type && !@mime_type.binary?
        "7bit"
      else
        "binary"
      end
    end

inline() click to toggle source

    # File lib/mail/attachments_list.rb, line 19
    def inline
      @content_disposition_type = 'inline'
      self
    end

set_mime_type(filename) click to toggle source

    # File lib/mail/attachments_list.rb, line 94
    def set_mime_type(filename)
      # Have to do this because MIME::Types is not Ruby 1.9 safe yet
      if RUBY_VERSION >= '1.9'
        filename = filename.encode(Encoding::UTF_8) if filename.respond_to?(:encode)
      end

      @mime_type = MIME::Types.type_for(filename).first
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.