# File lib/mailfactory.rb, line 275
  def add_attachment_as(file, emailfilename, type=nil, attachmentheaders = nil)
    attachment = Hash.new()
    attachment['filename'] = emailfilename

    if(type != nil)
      attachment['mimetype'] = type.to_s()
    elsif(file.kind_of?(String) or file.kind_of?(Pathname))
      attachment['mimetype'] = MIME::Types.type_for(file.to_s()).to_s
    else
      attachment['mimetype'] = ''
    end
    
    if(file.kind_of?(String) or file.kind_of?(Pathname))    
      # Open in rb mode to handle Windows, which mangles binary files opened in a text mode
      File.open(file.to_s(), "rb") { |fp|
        attachment['attachment'] = file_encode(fp.read())
      }
    elsif(file.respond_to?(:read))
      attachment['attachment'] = file_encode(file.read())
    else
      raise(Exception, "file is not a supported type (must be a String, Pathnamem, or support read method)")
    end
    
    if(attachmentheaders != nil)
      if(!attachmentheaders.kind_of?(Array))
        attachmentheaders = attachmentheaders.split(/\r?\n/)
      end
      attachment['headers'] = attachmentheaders
    end
    
    @attachments << attachment
  end