Array
# File lib/mail/attachments_list.rb, line 4 4: def initialize(parts_list) 5: @parts_list = parts_list 6: @content_disposition_type = 'attachment' 7: parts_list.map { |p| 8: if p.content_type == "message/rfc822" 9: Mail.new(p.body).attachments 10: elsif p.parts.empty? 11: p if p.attachment? 12: else 13: p.attachments 14: end 15: }.flatten.compact.each { |a| self << a } 16: self 17: end
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[1].filename #=> ‘test.jpg‘
# File lib/mail/attachments_list.rb, line 31 31: def [](index_value) 32: if index_value.is_a?(Fixnum) 33: self.fetch(index_value) 34: else 35: self.select { |a| a.filename == index_value }.first 36: end 37: end
# File lib/mail/attachments_list.rb, line 39 39: def []=(name, value) 40: encoded_name = Mail::Encodings.decode_encode name, :encode 41: default_values = { :content_type => "#{set_mime_type(name)}; filename=\"#{encoded_name}\"", 42: :content_transfer_encoding => "#{guess_encoding}", 43: :content_disposition => "#{@content_disposition_type}; filename=\"#{encoded_name}\"" } 44: 45: if value.is_a?(Hash) 46: 47: default_values[:body] = value.delete(:content) if value[:content] 48: 49: default_values[:body] = value.delete(:data) if value[:data] 50: 51: encoding = value.delete(:transfer_encoding) || value.delete(:encoding) 52: if encoding 53: if Mail::Encodings.defined? encoding 54: default_values[:content_transfer_encoding] = encoding 55: else 56: raise "Do not know how to handle Content Transfer Encoding #{encoding}, please choose either quoted-printable or base64" 57: end 58: end 59: 60: if value[:mime_type] 61: default_values[:content_type] = value.delete(:mime_type) 62: @mime_type = MIME::Types[default_values[:content_type]].first 63: default_values[:content_transfer_encoding] = guess_encoding 64: end 65: 66: hash = default_values.merge(value) 67: else 68: default_values[:body] = value 69: hash = default_values 70: end 71: 72: if hash[:body].respond_to? :force_encoding and hash[:body].respond_to? :valid_encoding? 73: if not hash[:body].valid_encoding? and default_values[:content_transfer_encoding].downcase == "binary" 74: hash[:body].force_encoding("BINARY") 75: end 76: end 77: 78: attachment = Part.new(hash) 79: attachment.add_content_id(hash[:content_id]) 80: 81: @parts_list << attachment 82: end
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 86: def guess_encoding 87: if @mime_type && !@mime_type.binary? 88: "7bit" 89: else 90: "binary" 91: end 92: end
# File lib/mail/attachments_list.rb, line 19 19: def inline 20: @content_disposition_type = 'inline' 21: self 22: end
# File lib/mail/attachments_list.rb, line 94 94: def set_mime_type(filename) 95: # Have to do this because MIME::Types is not Ruby 1.9 safe yet 96: if RUBY_VERSION >= '1.9' 97: filename = filename.encode(Encoding::UTF_8) if filename.respond_to?(:encode) 98: end 99: 100: @mime_type = MIME::Types.type_for(filename).first 101: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.