Created
August 8, 2011 19:36
-
-
Save vivien/1132525 to your computer and use it in GitHub Desktop.
Sample script for the mail gem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 [email protected] -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