The following is a simple script that is designed to run an application and, if the application produces any output, e-mail that application’s output to a given address. I use this for several of my custom scripts used for updates and backups. I have an e-mail mailbox setup just for logging purposes. To use this script, simply replace with the address of your local SMTP server.

#!/usr/bin/env ruby

require 'net/smtp'

def mailOutput(command,from,to,subject)

  msghead = "From: #{from}\nTo: #{to}\nSubject: #{subject}\n\n"
  msgstr = ""

  IO.popen(command+' 2>&1') { |f| msgstr +=  }
  if not msgstr.strip.empty? then
    smtp = Net::SMTP.start("",25)
    smtp.send_message msghead+msgstr,from,to


if ARGV.length != 4 then
  puts 'Usage: email_log.rb <command> <from> <to> <subject>'
  exit 1
  mailOutput ARGV[0],ARGV[1],ARGV[2],ARGV[3]
  exit 0