Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script I found at http://www.jayway.com/2010/01/16/scripting-in-ruby/ and I made a few "necessary" changes.
#!/usr/bin/env ruby
# Use the currently configured ruby version
# I could not get this to work on v1.8.7, only v1.9.3
# optparse contains OptionParser, ostruct: OpenStruct and growl Growl
require 'optparse'
require 'ostruct'
require 'growl'
# This is the name of the script that is called
# Whatever you name your script will be reflected here.
# This can be useful if you want to have the same script do many things
# Create differently named links to the script
# and use the program name to select behavior.
PROGRAM_NAME = $0
PROGRAM_VERSION = 1.0
# Create an OpenStruct to save the options.
# OpenStruct allows you to use options.my_option instead of
# option['my_option'] with a normal hash
# http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html
def options
@options ||= OpenStruct.new
end
# This is the options of the program, see OptionParser
# http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html
def program_options
[
# The values of the array are,
# [long_option, short_option and parameter, description, code to execute]
['--to', '-t TO', "Send email to recipient",
lambda { |value| options.to = value }
],
['--message', '-m MESSAGE', "Include the message.",
lambda { |value| options.message = value }
],
['--subject', '-s SUBJECT', "Include the subject.",
lambda { |value| options.subject = value }
],
['--verbose', '-v', "Log to standard output.",
lambda { |value| options.verbose = true }
],
['--version', '-V', "Display the program version.",
lambda { |value|
puts "#{program_name}, version #{PROGRAM_VERSION}"
exit
}
]
]
end
option_parser = OptionParser.new do |opts|
opts.banner = "#{PROGRAM_NAME} [options] attachments..."
opts.separator ""
opts.separator "Options are ..."
# Add the command on_tail, to make it appear as the last option in the list.
opts.on_tail("-h", "--help", "-H", "Display this help message.") do
puts opts
exit
end
program_options.each { |args| opts.on(*args) }
end
begin
# Parse the options and remove them from the ARGV array
option_parser.parse!
rescue OptionParser::ParseError => error
puts error.message
puts option_parser
exit
end
unless options.to
puts 'Missing required argument --to or -t'
puts option_parser
exit
end
options.subject = 'No subject' unless options.subject
options.message = '' unless options.message
# Concatenate the options into a proper command
command = 'sendemail -o tls=yes -s smtp.gmail.com:587 -f user@gmail.com -xu user@gmail.com -xp password '
command += "-t '#{options.to}' -u '#{options.subject}' -m '#{options.message}' "
# Append the filenames with the -a option to send them as attachments
# Only the non options (the filenames) are left in ARGV
unless ARGV.empty?
command += " -a #{ARGV.join(' ')}"
end
# Print the command to screen if using verbose mode.
puts command if options.verbose
system command
Growl.notify "#{options.subject}n#{options.message}", :icon => :jpeg, :title => "Email sent to #{options.to}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.