Skip to content

Instantly share code, notes, and snippets.

@n1zyy
Created September 8, 2011 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n1zyy/1203568 to your computer and use it in GitHub Desktop.
Save n1zyy/1203568 to your computer and use it in GitHub Desktop.
IMAP Subscriptions with Ruby's net/imap
require 'net/imap'
# There's a disconnect between the docs: http://ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html
# and reality. This is what works for me:
# Log in using SSL... Note that this syntax doesn't match the docs at all...
# 3rd option is use_ssl
imap = Net::IMAP.new('mail.example.com', 993, true)
imap.login('username', 'password')
# All folders/mailboxes
folders = imap.list('', "*")
# => [#<struct Net::IMAP::MailboxList attr=[:Hasnochildren], delim="/", name="aeolus-devel">, #<struct Net::IMAP::MailboxList attr=[:Hasnochildren], delim="/", name="announce-list"> ...]
# Subscriptions use similar syntax:
subs = imap.lsub('', "*")
# => [#<struct Net::IMAP::MailboxList attr=[], delim="/", name="aeolus-devel">, #<struct Net::IMAP::MailboxList attr=[], delim="/", name="Drafts"> ...]
# See what I'm not subscribed to:
no_subs = folders.collect{|x| x.name} - subs.collect{|x| x.name}
# => ["boston-list", "Chats", "Contacts", "Emailed Contacts", "INBOX/Sent", "Junk", "Trash"]
# Subscribe to some:
imap.subscribe('boston-list')
# Leave some sillier ones
imap.unsubscribe('Drafts')
# => #<struct Net::IMAP::TaggedResponse tag="RUBY0010", name="OK", data=#<struct Net::IMAP::ResponseText code=nil, text="UNSUBSCRIBE completed">, raw_data="RUBY0010 OK UNSUBSCRIBE completed\r\n">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment