Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to archive a message using ruby `net/imap`

Tags:

ruby

imap

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.

like image 602
JuanPablo Avatar asked Jul 16 '11 04:07

JuanPablo


1 Answers

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()
like image 55
NARKOZ Avatar answered Sep 19 '22 13:09

NARKOZ