bin/gmail
#!/usr/bin/env ruby
require 'rubygems'
require 'gmail_sender'
require 'choice'
require 'yaml'
Choice.options do
header ''
header 'options:'
option :receiver_email do
short '-t'
long '--to=*RECEIVERS'
desc 'A list of space separated receiver emails'
end
option :subject do
short '-s'
long '--subject=SUBJECT'
desc 'Subject of the email'
end
option :content do
short '-c'
long '--content=MAIL_CONTENTS'
desc 'Body of the email, uses STDIN if omitted'
end
option :content_type do
short '-ct'
long '--content_type=CONTENT_TYPE'
desc 'Content Type, text/plain by default, use text/html for html'
end
option :attachments do
short '-a'
long '--attachments *ATTACHMENTS'
desc "A list of space separated list of files you wish to attach."
end
end
def params
return @params if @params
config_file_path = File.join(ENV['HOME'],'.gmail')
if !File.exists?(config_file_path)
STDERR.puts "Please first create a ~/.gmail file with some defaults. Example:
receiver_email: default_receiver@gmail.com
sender_user: gmail_account_user_name
sender_password: gmail_account_user_password
"
exit 1
end
defaults = {'subject' => 'Sent from command line',
'sender_domain' => 'gmail.com',
'content_type' => 'text/plain'}
@params = defaults.merge(YAML.load_file(config_file_path)).merge(Choice.choices)
end
mailer = GmailSender.new("#{params['sender_user']}@#{params['sender_domain']}", params['sender_password'])
if params['attachments']
params['attachments'].each do |attachment|
mailer.attach(attachment)
end
end
content = params['content'] || (params['attachments'] && params['attachments'].join("\n"))
mailer.send(:to => params['receiver_email'], :subject => params['subject'], :content => content || STDIN.read, :content_type => params['content_type'])