-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsendgmail
executable file
·98 lines (79 loc) · 2.58 KB
/
sendgmail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env ruby
# Author: Anders Janmyr
require 'rubygems'
require 'optparse'
require 'ostruct'
require 'growl'
# This is the name of the script that is called
PROGRAM_NAME = $0
PROGRAM_VERSION = 1.0
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 recipent",
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
load "#{ENV['HOME']}/.gmail"
rescue LoadError
puts "You need to have GMAIL_USER and GMAIL_PASSWORD set in file '~/.gmail'"
exit
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 = 'No message' unless options.message
command = "sendemail -o -s smtp.gmail.com:587 -f #{GMAIL_USER} -xu #{GMAIL_USER} -xp #{GMAIL_PASSWORD}"
command += " -t '#{options.to}' -u '#{options.subject}' -m '#{options.message}' "
# Only the non options (the filenames) are left in ARGV
unless ARGV.empty?
command += " -a #{ARGV.join(' ')}"
end
puts command if options.verbose
system command
Growl.notify "#{options.subject}\n#{options.message}", :icon => :jpeg, :title => "Email sent to #{options.to}"