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: default_values = { :content_type => "#{set_mime_type(name)}; filename=\"#{name}\"", 41: :content_transfer_encoding => "#{guess_encoding}", 42: :content_disposition => "#{@content_disposition_type}; filename=\"#{name}\"" } 43: 44: if value.is_a?(Hash) 45: 46: default_values[:body] = value.delete(:content) if value[:content] 47: 48: default_values[:body] = value.delete(:data) if value[:data] 49: 50: encoding = value.delete(:transfer_encoding) || value.delete(:encoding) 51: if encoding 52: if Mail::Encodings.defined? encoding 53: default_values[:content_transfer_encoding] = encoding 54: else 55: raise "Do not know how to handle Content Transfer Encoding #{encoding}, please choose either quoted-printable or base64" 56: end 57: end 58: 59: if value[:mime_type] 60: default_values[:content_type] = value.delete(:mime_type) 61: @mime_type = MIME::Types[default_values[:content_type]].first 62: default_values[:content_transfer_encoding] = guess_encoding 63: end 64: 65: hash = default_values.merge(value) 66: else 67: default_values[:body] = value 68: hash = default_values 69: end 70: 71: if hash[:body].respond_to? :force_encoding and hash[:body].respond_to? :valid_encoding? 72: if not hash[:body].valid_encoding? and default_values[:content_transfer_encoding].downcase == "binary" 73: hash[:body].force_encoding("BINARY") 74: end 75: end 76: 77: attachment = Part.new(hash) 78: attachment.add_content_id(hash[:content_id]) 79: 80: @parts_list << attachment 81: 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 85 85: def guess_encoding 86: if @mime_type && !@mime_type.binary? 87: "7bit" 88: else 89: "binary" 90: end 91: 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 93 93: def set_mime_type(filename) 94: # Have to do this because MIME::Types is not Ruby 1.9 safe yet 95: if RUBY_VERSION >= '1.9' 96: new_file = String.new(filename).force_encoding(Encoding::BINARY) 97: ext = new_file.split('.'.force_encoding(Encoding::BINARY)).last 98: filename = "file.#{ext}".force_encoding('US-ASCII') 99: end 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.