How to archive a message using ruby `net/imap`
Asked Answered
F

1

7

With the following ruby code, I can read a user's mail in an inbox via IMAP:

require 'net/imap'
imap = Net::IMAP.new('imap.gmail.com',993,true)
imap.login('user','passwd')
imap.select('INBOX')
mailIds = imap.search(['ALL'])
mailIds.each do |id|
  msg = imap.fetch(id,'RFC822')[0].attr['RFC822']
  puts msg
end 
imap.logout()
imap.disconnect()

I want to know how I can archive and mark read emails. I want to move the emails out of the user's inbox.

Faldstool answered 16/7, 2011 at 4:9 Comment(0)
G
6

Use store method

require 'net/imap'
imap = Net::IMAP.new('imap.gmail.com', 993, true)
imap.login('user', 'passwd')
imap.select('INBOX')
mailIds = imap.search(['ALL'])
mailIds.each do |id|
  msg = imap.fetch(id, 'RFC822')[0].attr['RFC822']
  puts msg
  imap.store(id, "+FLAGS", [:Seen])
end 
imap.logout()
imap.disconnect()
Gil answered 16/7, 2011 at 4:34 Comment(2)
This doesn't seem to work on GMail boxes: STORE attempt on READ-ONLY folder (Failure)Sidekick
Have you explicitly selected a folder? It didn't work for me until I added the folder selection there (imap.select 'INBOX')Inhesion

© 2022 - 2024 — McMap. All rights reserved.