Ruby - IMAP 迭代多个文件夹/select 多个文件夹
Ruby - IMAP iterate multiple folders / select multiple folders
这是我的 IMAP 代码:
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
现在我只为“FOLDER1”设置了它,但是我有很多文件夹需要从中获取文件。是否可以添加多个文件夹?像
imap.examine("FOLDER1", "FOLDER2", "FOLDER3", "FOLDER4")
我通过制作数组解决了我的问题
array_of_folders = ["Folder1","Folder2","Folder3","Folder4"]
并做了一个.each
array_of_folders.each do |folders|
imap.examine(folders) # takes each folder
# Rest of the code
end
这是我的 IMAP 代码:
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
现在我只为“FOLDER1”设置了它,但是我有很多文件夹需要从中获取文件。是否可以添加多个文件夹?像
imap.examine("FOLDER1", "FOLDER2", "FOLDER3", "FOLDER4")
我通过制作数组解决了我的问题
array_of_folders = ["Folder1","Folder2","Folder3","Folder4"]
并做了一个.each
array_of_folders.each do |folders|
imap.examine(folders) # takes each folder
# Rest of the code
end