Eli Albért
Eli Albért

Reputation: 706

Select mailbox "sent mail" or "all mail" in Ruby Net::IMAP

I'm trying to use Net::IMAP in Ruby to search all mail sent by me, but I'm having trouble selecting anything other than INBOX.

imap.select('INBOX')

works fine, but

imap.select('Mail/sent-mail')

as shown on the Net::IMAP documentation gives me "Unknown Mailbox".

Incidentally, this is to be used with gmail.

I also tried adding "in", "anywhere" to my imap.search(), but that didn't parse.

Current code:

imap.select('INBOX')
now = Time.now.localtime - 1209600 #two weeks
since = now.day.to_s() + "-" + Date::MONTHNAMES[now.month] + "-" + now.year.to_s()
puts "since"
puts since
begin
  mail_ids = imap.search(["FROM", "me", "SINCE", since])
  mail_ids.each do |id|
    text = imap.fetch(id, 'BODY[HEADER.FIELDS (SUBJECT)]').to_s.split("{").second.chop
    puts text
  end
end

Upvotes: 20

Views: 11681

Answers (4)

z atef
z atef

Reputation: 7679

Don't use LIST "" *. You many end up with thousands of mailboxes. Use LIST "" % . If you are only interested in children/subfolders, you can do something like imap.list '', '%/%' and so on imap.list '', '%/%/%'

lists parent(s) folder only, depth 1.

C: RUBY0002 LIST "" "%"
S: * LIST (\HasNoChildren) "/" Calendar
S: * LIST (\HasNoChildren) "/" Contacts
S: * LIST (\HasNoChildren) "/" "Deleted Items"
S: * LIST (\HasNoChildren) "/" Drafts
S: * LIST (\Marked \HasChildren) "/" INBOX
S: * LIST (\HasNoChildren) "/" Journal
S: * LIST (\HasNoChildren) "/" "Junk E-Mail"
S: * LIST (\HasNoChildren) "/" Notes
S: * LIST (\HasNoChildren) "/" Outbox
S: * LIST (\HasNoChildren) "/" "Sent Items"
S: * LIST (\HasNoChildren) "/" Tasks
S: RUBY0002 OK LIST completed.

list children . depth 2.

C: RUBY0003 LIST "" "%/%"
S: * LIST (\HasNoChildren) "/" INBOX/subfolder
S: RUBY0003 OK LIST completed.
C: RUBY0004 SELECT INBOX/subfolder
S: * 2 EXISTS
S: * 0 RECENT
S: * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
S: * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
S: * OK [UIDVALIDITY 37286] UIDVALIDITY value
S: * OK [UIDNEXT 6] The next unique identifier value
S: RUBY0004 OK [READ-WRITE] SELECT completed.

Upvotes: 0

maček
maček

Reputation: 77778

I found the following to be helpful (ruby 2.0.0-p195)

# list all folders
imap.list '', '%'

Upvotes: 2

Jose Antonio Pio Gil
Jose Antonio Pio Gil

Reputation: 180

You can find the names of all folders with:

imap.list('*', '*') 

The Gmail folders name's will change depending on the user selected language. So in Spanish for example:

"[Gmail]/All" Mail will be "[Gmail]/Todos"

Upvotes: 10

dkarp
dkarp

Reputation: 14763

The "sent mail" folder will differ from provider to provider. Gmail's "sent mail" folder is named "[Gmail]/Sent Mail". Select that instead and it'll work.

imap.select('[Gmail]/Sent Mail')

FYI, Gmail's system folders are the following:

  • INBOX
  • [Gmail]/All Mail
  • [Gmail]/Drafts
  • [Gmail]/Sent Mail
  • [Gmail]/Spam
  • [Gmail]/Starred
  • [Gmail]/Trash

Upvotes: 37

Related Questions