Reputation: 313
This is my IMAP code:
imap = Net::IMAP.new('outlook.office365.com', 993, true)
imap.login('USERNAME', 'PASSWORD')
imap.examine("FOLDER1")
imap.search(["SINCE", "17-Dec-2020"]).each do |message_id|
envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
subject = Mail::Encodings.unquote_and_convert_to( envelope.subject, 'utf-8' )
next unless subject.include?("SOME TXT")
body = imap.fetch(message_id, "BODY[]")[0].attr["BODY[]"]
mail = Mail.new(body)
mail.attachments.each do |a|
next unless File.extname(a.filename) == ".pdf"
File.open("/path/to/file/pdfscript/#{a.filename}", 'wb') do |file|
file.write(a.body.decoded)
end
end
end
Right now I have it setup only for "FOLDER1", but I have a lot of folders that I need to get files from. Is it possible to add multiple folders? Something like
imap.examine("FOLDER1", "FOLDER2", "FOLDER3", "FOLDER4")
Upvotes: 1
Views: 142
Reputation: 313
I solved my problem by making an array
array_of_folders = ["Folder1","Folder2","Folder3","Folder4"]
and made an .each
array_of_folders.each do |folders|
imap.examine(folders) # takes each folder
# Rest of the code
end
Upvotes: 3