Sha256: ce3b88582309e52696dccc985314f1c4338df19912d9a4e727ca1625d394aa61

Contents?: true

Size: 1.31 KB

Versions: 113

Compression:

Stored size: 1.31 KB

Contents

require 'net/imap'

module Vmail
class ContactsExtractor
  def initialize(username, password)
    puts "logging as #{username}"
    @username, @password = username, password
  end

  def open
    @imap = Net::IMAP.new('imap.gmail.com', 993, true, nil, false)
    puts @imap.login(@username, @password)
    yield @imap
    @imap.close 
    @imap.disconnect
  end

  def extract(limit = 500)
    open do |imap|
      mailbox = '[Gmail]/Sent Mail'
      STDERR.puts "selecting #{mailbox}"
      imap.select(mailbox)
      STDERR.puts "fetching last #{limit} sent messages"
      all_uids = imap.uid_search('ALL')
      STDERR.puts "total messages: #{all_uids.size}"
      limit = [limit, all_uids.size].min
      STDERR.puts "extracting addresses from #{limit} of them"
      uids = all_uids[-limit ,limit]
      imap.uid_fetch(uids, ["FLAGS", "ENVELOPE"]).each do |fetch_data|
        recipients = fetch_data.attr["ENVELOPE"].to
        next unless recipients
        recipients.each do |address_struct|
          email = [address_struct.mailbox, address_struct.host].join('@') 
          name = address_struct.name
          if name 
            name = Mail::Encodings.unquote_and_convert_to(name, 'UTF-8') 
            yield "#{name} <#{email}>"
          else
            yield email
          end
        end
      end
    end
  end
end
end

Version data entries

113 entries across 113 versions & 1 rubygems

Version Path
vmail-1.3.5 lib/vmail/contacts_extractor.rb
vmail-1.3.4 lib/vmail/contacts_extractor.rb
vmail-1.3.2 lib/vmail/contacts_extractor.rb
vmail-1.3.0 lib/vmail/contacts_extractor.rb
vmail-1.2.9 lib/vmail/contacts_extractor.rb
vmail-1.2.8 lib/vmail/contacts_extractor.rb
vmail-1.2.6 lib/vmail/contacts_extractor.rb
vmail-1.2.5 lib/vmail/contacts_extractor.rb
vmail-1.2.4 lib/vmail/contacts_extractor.rb
vmail-1.2.3 lib/vmail/contacts_extractor.rb
vmail-1.2.2 lib/vmail/contacts_extractor.rb
vmail-1.2.1 lib/vmail/contacts_extractor.rb
vmail-1.2.0 lib/vmail/contacts_extractor.rb
vmail-1.1.9 lib/vmail/contacts_extractor.rb
vmail-1.1.8 lib/vmail/contacts_extractor.rb
vmail-1.1.7 lib/vmail/contacts_extractor.rb
vmail-1.1.6 lib/vmail/contacts_extractor.rb
vmail-1.1.5 lib/vmail/contacts_extractor.rb
vmail-1.1.4 lib/vmail/contacts_extractor.rb
vmail-1.1.3 lib/vmail/contacts_extractor.rb