Skip to content

Instantly share code, notes, and snippets.

@gpr
Created March 3, 2015 11:34
Show Gist options
  • Save gpr/a6276c0ed22056020982 to your computer and use it in GitHub Desktop.
Save gpr/a6276c0ed22056020982 to your computer and use it in GitHub Desktop.
Process email attachments in Rails
# message is Mail::Message
# see http://www.rubydoc.info/github/mikel/mail/Mail/Message
message.attachments.each do |attch|
f = Tempfile.new(attch.filename, Rails.root.join('tmp'))
puts " - Save #{attch.filename} to #{f.path}"
begin
f.binmode
f.write attch.decoded
ensure
file.close
file.unlink # deletes the temp file
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment