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

4

19

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
Slayton answered 3/3, 2011 at 21:29 Comment(0)
K
36

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
Kery answered 3/3, 2011 at 21:35 Comment(4)
Note that these folder names are localized. You can use the XLIST command to get the folder type.Goaltender
Beautiful, have been looking for this all over the place, thanks you! How can I select All Mail regardless of the language then?Ba
Issue an XLIST "" * command and select the one that comes back tagged with \AllMail.Kery
This list of system folders worked for me in Perl as well. Thanks. :-)Bedridden
B
10

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"

Bant answered 29/10, 2011 at 18:53 Comment(2)
Beautiful, have been looking for this all over the place, thanks you! How can I select All Mail regardless of the language then?Ba
How about imap_cli.list('', '*').select { |k| k[:attr].include?(:All) }.first?Streamline
N
2

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

# list all folders
imap.list '', '%'
Norbertonorbie answered 21/6, 2013 at 22:2 Comment(0)
M
0

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.
Maccabees answered 8/9, 2016 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.