开发者

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

开发者 https://www.devze.com 2023-03-20 13:10 出处:网络
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)

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()
开发者_C百科

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


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消