Scripting in Ruby

I just read, or rather skimmed, the book, called Everyday Scripting with Ruby and it is awful. I had high expectations. I was expecting something like
Perl for System Administration, where you right away get into hard core Perl scripting. This book is nothing like that! It is a really basic introduction to Ruby, and if you have any experience programming at all, DON’T BUY THIS BOOK.

OK, enough complaining! I am going to do what this book does not do. I am going to show a good way to write scripts in Ruby. If you want to learn more about Ruby, buy
Programming Ruby, The Well-Grounded Rubyist and The Ruby Way.

Ruby is an excellent language. It is, in my opinion, the best scripting language and it beats both Perl and Python face down. If you are not scripting in Ruby, you are missing out.
Ok, so here is the script.

sendgmail

The script is called sendgmail and it sends mail via gmail (surprise!).

This is how the command is used.

$ sendgmail --help
/usr/local/bin/sendgmail [options] attachments...
Options are ...
    -t, --to TO                      Send email to recipient
    -m, --message MESSAGE            Include the message.
    -s, --subject SUBJECT            Include the subject.
    -v, --verbose                    Log to standard output.
    -V, --version                    Display the program version.
    -h, -H, --help                   Display this help message.

And here is the code, liberally sprinkled with comments.

#!/usr/bin/env ruby # Use the currently configured ruby version 
# 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 -f from@gmail.com -xu from@gmail.com -xp secret '
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}"

Install required dependencies on OS X

The script requires you to install sendemail.

# Install sendemail and capabilty to send secure mail, required by Gmail 
sudo port install sendemail p5-net-ssleay p5-io-socket-ssl

# or download from http://caspian.dotconf.net/menu/Software/SendEmail/

It also requires you to install Growl. The latest version 2.0 can be bought from the Mac App Store. The growlnotify command line utility can be downloaded fromGrowl Downloads. The gem that uses this utility, also conveniently named growl can be installed as normal gems.

# Install the growl gem 
sudo gem install growl

That’s it, a simple script that will allow you to send messages and files through
gmail from the command line.

This Post Has 4 Comments

  1. Awesome. This script is a great application. Ruby it’s fantastic. Thanks!

  2. This is what I’ve looked for!!!! Thank you very much!

  3. This worked great, thank you. A few notes to add…

    1.) A space needs to be added to the concatenation so the two lines are separated by a space.
    2.) For those who aren’t using port, you can simply download the tar.gz from http://caspian.dotconf.net/menu/Software/SendEmail/ and place the perl script in your PATH.
    3.) I opted to use port 587 because port 25 is blocked by many ISPs.

    Gist with updates: https://gist.github.com/4535503

  4. Jason, thanks for the feedback. I’m glad you liked it.
    I will update as per your suggestions.

Leave a Reply

Close Menu