Tagged as attachments

edit delete favorite
Posted by eric on Nov 27, 2007
Make content encoding adjustments as necessary... I always assume base64 encoding - can decode others using string's unpack method. To use the enable_ssl method, you will need to use the most recent net/pop Ruby version in trunk http://svn.ruby-lang.org/repos/ruby/trunk/lib/net/pop.rb [svn.ruby-lang.org]... just copy it to your current project's lib.
require 'net/pop'

BASE_DIR = "/tmp/attachements"

Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
Net::POP3.start('pop.gmail.com', Net::POP3.default_pop3s_port, 'email@gmail.com', 'passwd') do |pop|
  if pop.mails.empty?
    puts 'No mail.'
  else
    pop.each_mail do |email|
      msg = email.pop

      msg.scan(/content-type: application\/octet-stream;\s*name=(.*?)\s*content-transfer-encoding: base64(.+?)(---)?/m) do |v|
      filename = $1.to_s
      data = $2.to_s.strip.unpack('m*')
      File.open("#{BASE_DIR}/#{filename}", File::CREAT|File::TRUNC|File::WRONLY) do |f|
        f.write(data)
      end
    end
  end
end
 
Please submit any bugs/features and report abuse. Thanks!
Spacer
Spacer
Spacer
Top Tags
Spacer