Skip to content

Instantly share code, notes, and snippets.

@vivien
Created August 8, 2011 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vivien/1132525 to your computer and use it in GitHub Desktop.
Save vivien/1132525 to your computer and use it in GitHub Desktop.
Sample script for the mail gem.
#!/usr/bin/env ruby
require "mail"
require "optparse"
imap_opts = {
:address => "imap.gmail.com",
:port => 993,
:enable_ssl => true
}
ARGV.options do |o|
o.banner = "Usage:\n #{$0} [options]"
o.on_head("Options:")
o.on('-s', '--imap-server=ADDR', String, "Imap server") { |s| imap_opts[:address] = s }
o.on('-p', '--imap-password=PASS', String, "Imap password") { |p| imap_opts[:password] = p }
o.on('-u', '--imap-user=USER', String, "Imap user name") { |u| imap_opts[:user_name] = u }
o.on_tail("\nExample:\n #{$0} -u john.doe@gmail.com -p xxx")
end
begin
ARGV.options.parse!
if imap_opts[:user_name].nil? || imap_opts[:password].nil?
puts ARGV.options
raise ArgumentError rescue exit
end
Mail.defaults do
retriever_method :imap, imap_opts
end
emails = Mail.find(:what => :last, :count => 11, :order => :dsc)
emails.each do |mail|
puts "\"#{mail.subject}\" from #{mail.from.first}"
end
end
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment