Created
August 26, 2011 21:01
-
-
Save anolson/1174436 to your computer and use it in GitHub Desktop.
Upload a file to Strava
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/smtp' | |
require 'optparse' | |
module SendFile | |
class CommandlineOptions | |
attr_accessor :options | |
def initialize() | |
@options = {:tls => true, :recipient => '[email protected]'} | |
end | |
def self.parse | |
parser = self.new | |
parser.parse | |
parser.options | |
end | |
def parse() | |
parser = OptionParser.new("Usage: sendfile.rb [options]") do |opts| | |
smtp_server_option(opts) | |
no_tls_option(opts) | |
password_option(opts) | |
from_option(opts) | |
filename_option(opts) | |
end | |
begin | |
parser.parse! | |
require_mandatory_options [:server, :password, :from, :filename] | |
auto_assign_options | |
rescue OptionParser::MissingArgument | |
puts parser | |
exit | |
end | |
end | |
def smtp_server_option(opts) | |
opts.on("-s", "--smtp-server SERVER", "SMTP Server") do |server| | |
@options[:server] = server | |
end | |
end | |
def password_option(opts) | |
opts.on("-p", "--password PASSWORD", "Password") do |password| | |
@options[:password] = password | |
end | |
end | |
def no_tls_option(opts) | |
opts.on("--no-tls", "Don't Require TLS") do |tls| | |
@options[:tls] = false | |
end | |
end | |
def from_option(opts) | |
opts.on("-a", "--from-address ADDRESS", "From Address") do |from| | |
@options[:from] = from | |
end | |
end | |
def filename_option(opts) | |
opts.on("-f", "--filename FILENAME", "File to send") do |filename| | |
@options[:filename] = filename | |
end | |
end | |
def require_mandatory_options(mandatory = []) | |
missing = mandatory.select{ |param| @options[param].nil? } | |
unless missing.empty? | |
puts "Missing options: #{missing.join(', ')}" | |
raise OptionParser::MissingArgument | |
end | |
end | |
def auto_assign_options() | |
@options[:tls] ? @options[:port] = 587 : @options[:port] = 25 | |
@options[:domain] = @options[:from].split("@")[1] | |
end | |
end | |
class Mailer | |
def self.send | |
options = SendFile::CommandlineOptions.parse | |
message = Message.new(options) | |
smtp = Net::SMTP.new(options[:server], options[:port]) | |
smtp.enable_starttls() if options[:tls] | |
smtp.start(options[:domain], options[:from], options[:password], :login) | |
smtp.send_message(message.to_s, options[:from], options[:recipient]) | |
end | |
end | |
class Message | |
def initialize(options = {}) | |
@filename = options[:filename] | |
@recipient = options[:recipient] | |
end | |
def to_s | |
boundary = (0...12).map{ ('a'..'z').to_a[rand(26)] }.join | |
message = <<MESSAGE_BODY | |
To: #{@recipient} | |
MIME-Version: 1.0 | |
Content-Type: multipart/mixed; boundary=#{boundary} | |
--#{boundary} | |
Content-Type: application/octet-stream; name="#{basename}" | |
Content-Disposition: attachment; filename="#{basename}" | |
Content-Transfer-Encoding:base64 | |
#{encoded_content} | |
--#{boundary}-- | |
MESSAGE_BODY | |
end | |
def encoded_content | |
[File.read(@filename)].pack("m") | |
end | |
def basename | |
File.basename(@filename) | |
end | |
end | |
end | |
SendFile::Mailer.send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment