Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Ruby shell script to create Entourage emails from the command line -
# Mail script for sending stuff from the command line through entourage
# which makes life super easy for sending files, or piping files to a mail body, etc.
# email -t bosses -c tracker -a ~/report.csv <SUBJECT HERE>
# Author:: Rob Hurring
# Date:: 2011-11-15
# Version:: 1.0a
require 'optparse'
require 'fcntl'
# aliases for users, can be a string or array here
UserAliases = {
'bosses' => %w{},
'me' => '',
'tracker' => ''
# compiles all addresses and maps our aliases
def compile_addresses(addresses)
addresses.inject([]) do |all, address|
address = UserAliases[address] if UserAliases.keys.include?(address)
all |= Array(address)
end.join(', ')
# Parse our options
options = {:to => [], :cc => [], :attachments => []} do |opts|
opts.banner = "Usage: #{$0} [options] "
opts.on('-t', '--to=TO', 'To'){ |t| options[:to] |= t.split(',') }
opts.on('-c', '--cc=CC', 'CC'){ |c| options[:cc] |= c.split(',') }
opts.on('-a', '--attach=ATTACHMENT', 'Attach'){ |a| options[:attachments] << File.expand_path(a) }
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit 1
# Get our subject and body (if STDIN is being used)
# Note: we DONT prompt for STDIN if nothing is piped in - it will just
# open a blank email with the rest filled out
subject = ARGV.join(' ') || ''
body = if STDIN.fcntl(Fcntl::F_GETFL, 0) == 0
body.gsub!(/"/, "\\\"") unless body.nil?
# Compile script
to = compile_addresses(options[:to])
cc = compile_addresses(options[:cc])
attachments = options[:attachments].inject([]) do |all, attachment|
attachment.gsub! /\//,':'
all << %|make new attachment at msg with properties {file:"#{attachment}"}|
script =<<-EOS
tell application "Microsoft Entourage"
set msg to make new draft window with properties {subject:"#{subject}", content:"#{body}", to recipients:"#{to}", cc recipients:"#{cc}"}
end tell
# Run Script
`osascript -e '#{script}' 2>/dev/null`
# email your bosses some reports
email -t bosses -a reports.tar.gz "Reports for you"
# email your tracker with some bad news
tail -n10 error_log|email -t tracker -a screenshot.png "BUG: something ut-ohd"

This comment has been minimized.

Copy link

@helios helios commented Nov 24, 2011

This is a great utility!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment