Skip to content

Instantly share code, notes, and snippets.

@daxadax
Created October 11, 2017 12:52
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 daxadax/865bd2212bb9bf09ced511e1192b3aa6 to your computer and use it in GitHub Desktop.
Save daxadax/865bd2212bb9bf09ced511e1192b3aa6 to your computer and use it in GitHub Desktop.
require 'gmail'
require 'csv'
require 'io/console'
class EmailExtractor
def self.run
new.run
end
def initialize
printf "Enter your gmail username: "
username = gets.chomp
printf "Enter you password: "
password = STDIN.noecho(&:gets).chomp
@gmail = Gmail.new(username, password)
end
def run
printf "\nConnecting..."
printf "\nCollecting emails..."
all_messages
printf "\nWriting to CSV..."
CSV.open("emails_test.csv", "w+") do |csv|
csv << %w[emails]
all_messages.uniq.each_with_index do |email, i|
csv << [email]
printf '.' if (i%15).zero?
end
end
printf "\nWriting complete."
printf "\nLogging you out..."
gmail.logout
printf "\nGoodbye.\n"
end
private
attr_reader :gmail
def all_messages
sent + all_mail
end
def all_mail
@all_mail ||= gmail.mailbox("[Gmail]/All Mail").emails.map(&:from).uniq
end
def sent
@sent ||= gmail.mailbox("[Gmail]/Sent Mail").emails.map(&:to).uniq
end
end
EmailExtractor.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment