Saving 1 or more file attachments using the Mail gem

require 'mail'

Mail.defaults do
  retriever_method :pop3, { :address             => "mail.jamesrobertson.eu",
                            :port                => 110,
                            :user_name           => 'james',
                            :password            => '*****',
                            :enable_ssl          => false }
end

email = Mail.all
email.attachments.each {|x| File.write x.filename, x.body.decoded }

The last line of this example saves each attachment to a file in the current file directory.

Note: The Mail::Message#multipart? method indicates if there is attachments or not.

Resources