Skip to content

Instantly share code, notes, and snippets.

@ybenjo
Created June 4, 2011 12:22
Show Gist options
  • Save ybenjo/1007855 to your computer and use it in GitHub Desktop.
Save ybenjo/1007855 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# usage: ruby report_zangyou.rb --time 1h20m --day 6/6
require "rubygems"
require "tmail"
require "tlsmail"
require "net/smtp"
require "net/pop"
require "kconv"
require "yaml"
require "optparse"
def make_mail(params)
mail = TMail::Mail.new
mail.to = params["to"]
mail.cc = params["cc"]
mail.from = params["from"]
mail.subject = params["subject"].tojis
mail.body = params["body"].tojis
mail.date = Time.now
mail.mime_version = "1.0"
mail.set_content_type "text", "plain", {"charset" => "iso-2022-jp"}
mail.transfer_encoding = "7bit"
return mail
end
def send_mail(mail)
# confirm password
print "Enter password: "
password = gets.chomp
# confirm mail
puts "body is: "
puts mail.body.toutf8
print "Are you sure? [Y/n]"
if gets != "Y"
puts "Cancel."
exit 1
end
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start("smtp.gmail.com", 587, "localhost.localdomain",
mail.from, password, "plain") do |smtp|
smtp.sendmail(mail.encoded, mail.from, mail.to)
end
end
def parse_time(str)
if !str.include?("h") && !str.include?("m")
puts "Invalid 残業: #{str}"
exit 1
end
time = ""
if str.include?("h")
time += str.scan(/(\d+)?h/)[0][0] + "時間"
end
if str.include?("m")
time += str.scan(/(\d+)?m/)[0][0] + "分"
end
return time
end
if __FILE__ == $0
config = YAML.load_file("./config.yaml")
OptionParser.new {|opt|
opt.on('--time VAL') {|v| config["time"] = parse_time(v)}
opt.on('--day VAL') {|v| config["day"] = v}
opt.parse!(ARGV)
}
config["subject"].sub!(/\$day/, config["day"])
config["body"].sub!(/\$day/, config["day"])
config["body"].sub!(/\$time/, config["time"])
send_mail(make_mail(config))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment