Select Page
NOTE: This is a static archive of an old blog, no interactions like search or categories are current.

I’ve had to help a client pull out sender addresses from a folder on a Zimbra server, Ruby supports IMAP but only with LOGIN and CRAM-MD5 methods while Zimbra wants PLAIN. Net::IMAP supports adding a new authenticator so it was pretty simple in the end:

class ImapPlainAuthenticator
  def process(data)
    return "#{@user}\0#{@user}\0#{@password}"
  end
 
  def initialize(user, password)
    @user = user
    @password = password
  end
end
 
Net::IMAP::add_authenticator('PLAIN', ImapPlainAuthenticator)

And using it to retrieve the list of senders:

imap = Net::IMAP.new('imap.your.com')
 
imap.authenticate('PLAIN', 'username', 'pass')
imap.examine('INBOX/subfolder')
imap.search(["ALL"]).each do |message_id|
    envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
    puts "#{envelope.from[0].mailbox}"
end

Very simple, took about 5 minutes and saved days of manual pain and suffering, result!