Sha256: fc9ab8fb63e2af6e5c9db04edcc6ea749f441abbfa3533b3b6775dcc9ad6278a

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 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 %Q("#{name}" <#{email}>)
          else
            yield email
          end
        end
      end
    end
  end
end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vmail-1.4.3 lib/vmail/contacts_extractor.rb
vmail-1.4.2 lib/vmail/contacts_extractor.rb
vmail-1.4.1 lib/vmail/contacts_extractor.rb
vmail-1.4.0 lib/vmail/contacts_extractor.rb
vmail-1.3.9 lib/vmail/contacts_extractor.rb
vmail-1.3.8 lib/vmail/contacts_extractor.rb
vmail-1.3.7 lib/vmail/contacts_extractor.rb
vmail-1.3.6 lib/vmail/contacts_extractor.rb