Skip to content

Instantly share code, notes, and snippets.

@n1zyy n1zyy/gist:1203568
Created Sep 8, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.