Skip to content

Instantly share code, notes, and snippets.

@ushitora-anqou
Last active May 14, 2020 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ushitora-anqou/726e23e1d8b08690753caddb9bdc31e7 to your computer and use it in GitHub Desktop.
Save ushitora-anqou/726e23e1d8b08690753caddb9bdc31e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require "base64"
require "optparse"
params = {
from: nil,
to: [],
subject: "",
message: nil,
attachments: [],
}
# Parse command-line arguments into params
opts = OptionParser.new do |opts|
opts.on("-f FROM") { |v| params[:from] = v }
opts.on("-t TO") { |v| params[:to].push(v) }
opts.on("-s SUBJECT") { |v| params[:subject] = v }
opts.on("-a ATTACHMENT") { |v| params[:attachments].push(v) }
opts.on("-m MESSAGE") { |v| params[:message] = v }
end
begin
opts.parse!
raise OptionParser::MissingArgument.new("-t") if params[:to].empty?
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
$stderr.puts $!.to_s
$stderr.puts opts
exit 1
end
params[:from] ||= "hoge@piyo.com" # FIXME
params[:message] ||= $stdin.read
IO.popen("/usr/sbin/sendmail -i -t", "w") do |sm|
# From: Alice <alice@example.com>
sm.puts "From: #{params[:from]}"
# To: Bob <bob@example.com>,Charley <charley@example.com>
sm.puts "To: #{params[:to].join(",")}"
# Subject: Hello, Bob
sm.puts "Subject: #{params[:subject]}"
# <empty line>
sm.puts
# <message>
sm.puts params[:message]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment