# File lib/mailfactory.rb, line 244
  def add_attachment(filename, type=nil, attachmentheaders = nil)
    attachment = Hash.new()
    attachment['filename'] = Pathname.new(filename).basename
    if(type == nil)
      attachment['mimetype'] = MIME::Types.type_for(filename).to_s
    else
      attachment['mimetype'] = type
    end 
    
    # Open in rb mode to handle Windows, which mangles binary files opened in a text mode
    File.open(filename, "rb") { |fp|
      attachment['attachment'] = file_encode(fp.read())
    }

    if(attachmentheaders != nil)
      if(!attachmentheaders.kind_of?(Array))
        attachmentheaders = attachmentheaders.split(/\r?\n/)
      end
      attachment['headers'] = attachmentheaders
    end

    @attachments << attachment
  end