Class: Mail::AttachmentsList

Inherits:
Array
  • Object
show all
Defined in:
lib/mail/attachments_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(parts_list) ⇒ AttachmentsList

Returns a new instance of AttachmentsList.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mail/attachments_list.rb', line 5

def initialize(parts_list)
  @parts_list = parts_list
  @content_disposition_type = 'attachment'
  parts_list.map { |p|
    if p.mime_type == 'message/rfc822'
      parts = []
      parts << p if p.attachment?
      parts.concat Mail.new(p.body.encoded).attachments
    elsif p.parts.empty?
      p if p.attachment?
    else
      p.attachments
    end
  }.flatten.compact.each { |a| self << a }
  self
end

Instance Method Details

#[](index_value) ⇒ Object

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’



34
35
36
37
38
39
40
# File 'lib/mail/attachments_list.rb', line 34

def [](index_value)
  if index_value.is_a?(Integer)
    self.fetch(index_value)
  else
    self.select { |a| a.filename == index_value }.first
  end
end

#[]=(name, value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mail/attachments_list.rb', line 42

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)
    if path = value.delete(:filename)
      value[:content] ||= File.open(path, 'rb') { |f| f.read }
    end

    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 = MiniMime.lookup_by_content_type(default_values[:content_type])
      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].casecmp('binary').zero?
      hash[:body] = hash[:body].dup if hash[:body].frozen?
      hash[:body].force_encoding("BINARY")
    end
  end

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

  @parts_list << attachment
end

#guess_encodingObject

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



93
94
95
96
97
98
99
# File 'lib/mail/attachments_list.rb', line 93

def guess_encoding
  if @mime_type && !@mime_type.binary?
    "7bit"
  else
    "binary"
  end
end

#inlineObject



22
23
24
25
# File 'lib/mail/attachments_list.rb', line 22

def inline
  @content_disposition_type = 'inline'
  self
end

#set_mime_type(filename) ⇒ Object



101
102
103
104
105
106
# File 'lib/mail/attachments_list.rb', line 101

def set_mime_type(filename)
  filename = filename.encode(Encoding::UTF_8) if filename.respond_to?(:encode)

  @mime_type = MiniMime.lookup_by_filename(filename)
  @mime_type && @mime_type.content_type
end