Download attachments from gamil by using tmail.
#! /usr/bin/ruby
require 'net/imap'
require 'rubygems'
require 'tmail'
require 'fileutils'
require 'ftools'
# This is a convenience monkey patch
class Net::IMAP
def uid_move(uid, mailbox)
uid_copy(uid, mailbox)
uid_store(uid, "+FLAGS", [:Deleted])
end
end
server = 'imap.gmail.com'
username = 'gmail email id'
password = 'gmail password'
folder = 'INBOX'
movefolder = 'downloaded_resumes'
downloadfolder = "Folder_"
imap = Net::IMAP.new(server, 993, true)
imap.login(username, password)
imap.select(folder)
#puts imap.uid_search(["NOT", "SEEN"]).length
count=0
imap.uid_search(["NOT", "SEEN"]).reverse.each do |uid|
file_name="resume_"
no_of_resume=1
# save_attachment
mail = TMail::Mail.parse( imap.uid_fetch(uid, 'RFC822').first.attr['RFC822'])
if ! mail.attachments.blank?
#folder dynamic creation
if ((count%no_of_resume)==0 )
dirname="Resume/resume#{count/no_of_resume +1}"
File.makedirs(dirname)
else
dirname="Resume/resume#{count/no_of_resume +1}"
end
#folder dynamic creation
File.open(mail.attachments.first.original_filename,"w+") { |local_file|
local_file << mail.attachments.first.gets(nil)
#puts local_file.name.inspect
path=local_file.path
filename=File.basename(path)
extname=File.extname(filename)
loc="resume_#{count}#{extname}"
File.rename(filename, loc)
newfilename=File.basename(loc)
FileUtils.mv(newfilename, "Resume/resume#{count/no_of_resume +1}") # Move file one place to anouther place
count=count +1
}
end
# archive mail to mailbox
imap.uid_move(uid,movefolder)
end
imap.expunge
imap.logout
If you any quries mail me(palpandi26@gmail.com)
#! /usr/bin/ruby
require 'net/imap'
require 'rubygems'
require 'tmail'
require 'fileutils'
require 'ftools'
# This is a convenience monkey patch
class Net::IMAP
def uid_move(uid, mailbox)
uid_copy(uid, mailbox)
uid_store(uid, "+FLAGS", [:Deleted])
end
end
server = 'imap.gmail.com'
username = 'gmail email id'
password = 'gmail password'
folder = 'INBOX'
movefolder = 'downloaded_resumes'
downloadfolder = "Folder_"
imap = Net::IMAP.new(server, 993, true)
imap.login(username, password)
imap.select(folder)
#puts imap.uid_search(["NOT", "SEEN"]).length
count=0
imap.uid_search(["NOT", "SEEN"]).reverse.each do |uid|
file_name="resume_"
no_of_resume=1
# save_attachment
mail = TMail::Mail.parse( imap.uid_fetch(uid, 'RFC822').first.attr['RFC822'])
if ! mail.attachments.blank?
#folder dynamic creation
if ((count%no_of_resume)==0 )
dirname="Resume/resume#{count/no_of_resume +1}"
File.makedirs(dirname)
else
dirname="Resume/resume#{count/no_of_resume +1}"
end
#folder dynamic creation
File.open(mail.attachments.first.original_filename,"w+") { |local_file|
local_file << mail.attachments.first.gets(nil)
#puts local_file.name.inspect
path=local_file.path
filename=File.basename(path)
extname=File.extname(filename)
loc="resume_#{count}#{extname}"
File.rename(filename, loc)
newfilename=File.basename(loc)
FileUtils.mv(newfilename, "Resume/resume#{count/no_of_resume +1}") # Move file one place to anouther place
count=count +1
}
end
# archive mail to mailbox
imap.uid_move(uid,movefolder)
end
imap.expunge
imap.logout
If you any quries mail me(palpandi26@gmail.com)
No comments:
Post a Comment