Skip to content

Instantly share code, notes, and snippets.

@cieux1
Created August 9, 2013 09:25
Show Gist options
  • Select an option

  • Save cieux1/6192368 to your computer and use it in GitHub Desktop.

Select an option

Save cieux1/6192368 to your computer and use it in GitHub Desktop.
require 'gmail'
require 'date'
def fetch_attachmets(id, pass, label_name, from_date)
dir_name = "#{Dir.pwd}/#{Date.today}"
Dir.mkdir(dir_name) unless File.exists?(dir_name)
begin
Gmail.connect(id, pass) do |gmail|
puts gmail.logged_in? ? "login OK" : "NOT Logged in!!!!"
puts "Checking mails after #{from_date}..."
emails = gmail.label(label_name).find(:after => from_date)
puts "found #{emails.count.to_i} mails in label '#{label_name}'"
emails.each do |email|
if !email.message.attachments.empty?
puts email.subject
email.message.attachments.each do |f|
File.write(File.join(dir_name, f.filename), f.body.decoded)
end
# email.label!("downloaded")
end
end
end
rescue => e
puts "ERROR DETH!!!! (#{e.message}, #{e.backtrace})"
end
puts "DONE!!!"
end
def prompt_login
id = "**email**"
pass = "**pass**"
label_name = "**label name**"
# puts "Enter your email address"
# # id = gets.chomp
# puts "Enter password"
# # pass = gets.chomp
# puts "label name?"
# # label_name = gets.chomp
puts "How many days from today? (HANKAKU SUJI de PLS)"
duration = gets.chomp
from_date = Date.today - (duration.to_i)
fetch_attachmets(id, pass, label_name, from_date)
end
prompt_login
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment