-
-
Save sessa/3846072 to your computer and use it in GitHub Desktop.
A ruby emailer
This file contains hidden or 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
# http://ruby-doc.org/stdlib-1.9.3/libdoc/net/smtp/rdoc/Net/SMTP.html | |
require 'digest/md5' | |
require 'mime/types' | |
require 'net/smtp' | |
require 'optparse' | |
require 'ostruct' | |
require 'yaml' | |
class Emailer | |
attr_accessor :email, :message, :options, :server | |
def parse_options(args) | |
@options = OpenStruct.new | |
@options.email_conf = "#{ Dir.pwd }#{ File::SEPARATOR }emailer_conf.yml" | |
opt_parse = OptionParser.new do |opts| | |
opts.banner = "Usage: emailer.rb [options]" | |
opts.on('-a', '--attachment [FILE]', 'Attach a file: "~/john_doe.vcf"') do |file| | |
@options.attachment = file | |
end | |
opts.on('-c', '--conf [FILE]', 'Configuration file [optional], defaults to ./emailer_conf.yml') do |file| | |
@options.email_conf = file | |
end | |
opts.on('-f', '--from [FROM]', 'From address: "John Doe, [email protected]"') do |from| | |
@options.from = from | |
end | |
opts.on('-r', '--rcpt [RCPT]', 'Recipient address: "Jane Doe, [email protected]"') do |rcpt| | |
@options.rcpt = rcpt | |
end | |
opts.on('-s', '--subject [SUBJECT]', 'The message subject: "Just touching base..."') do |subject| | |
@options.subject = subject | |
end | |
opts.on('-m', '--message [MESSAGE]', 'The message: "Give me a call when you have a few minutes."') do |message| | |
@options.message = message | |
end | |
opts.on('--message-file [FILE]', 'Use an existing text file as message body: "~/my_message.txt"') do |file| | |
@options.message_file = file | |
end | |
opts.on_tail('-h', '--help', 'Display this screen') do | |
puts opts | |
exit 0 | |
end | |
end | |
begin | |
opt_parse.parse!(args) | |
rescue OptionParser::InvalidOption | |
puts "emailer: #{$!.message}" | |
puts "emailer: try 'emailer.rb --help' for more information" | |
exit 1 | |
end | |
end | |
def prepare_email | |
@email = OpenStruct.new | |
@email.from = @options.from[@options.from.index(',')+1..-1] | |
@email.from_alias = @options.from[0..@options.from.index(',')-1] | |
@email.rcpt = @options.rcpt[@options.rcpt.index(',')+1..-1] | |
@email.rcpt_alias = @options.rcpt[0..@options.rcpt.index(',')-1] | |
@email.subject = @options.subject | |
# TODO: Test this... | |
@email.message = @options.message + "\n" unless @options.message.nil? | |
unless @options.message_file.nil? || !File.exists?(@options.message_file) | |
File.open(@options.message_file, 'r') { |f| @email.message << f.readline } | |
end | |
unless @options.attachment.nil? || !File.exists?(@options.attachment) | |
attachment = File.read(@options.attachment) | |
encoded_attachment = [attachment].pack('m') | |
end | |
boundary = Digest::MD5.hexdigest(Time.now.to_s) | |
content_type = MIME::Types.type_for(@options.attachment) | |
@message = <<MESSAGE | |
From: #{@email.from_alias} <#{@email.from}> | |
To: #{@email.rcpt_alias} <#{@email.rcpt}> | |
Subject: #{@email.subject} | |
MIME-Version: 1.0 | |
Content-Type: multipart/mixed; boundary=#{boundary} | |
--#{boundary} | |
Content-Type: text/plain | |
Content-Transfer-Encoding: 8bit | |
#{@email.message} | |
--#{boundary} | |
Content-Type: #{content_type}; name=\"#{File.basename @options.attachment}\" | |
Content-Transfer-Encoding: base64 | |
Content-Disposition: attachment; filename="#{File.basename @options.attachment}" | |
#{encoded_attachment} | |
--#{boundary}-- | |
MESSAGE | |
end | |
def prepare_server_conf | |
server = YAML::load(File.open(@options.email_conf)) if File.exists? @options.email_conf | |
@server = OpenStruct.new | |
@server.host = server['host'] | |
@server.port = server['port'] || 25 | |
@server.fqdn = server['fqdn'] | |
@server.user = server['user'] | |
@server.pass = server['pass'] | |
@server.type = server['type'] || 'plain' | |
end | |
def send_email | |
begin | |
# Net::SMTP.start(@server.host, @server.port, @server.fqdn, @server.user, @server.pass, @server.type.to_sym) do |smtp| | |
Net::SMTP.start('localhost') do |smtp| | |
#smtp.enable_starttls_auto | |
smtp.send_message @message, @email.from, @email.rcpt | |
end | |
rescue Exception => e | |
puts "Exception: #{e}" | |
end | |
end | |
end | |
emailer = Emailer.new | |
emailer.parse_options(ARGV) | |
emailer.prepare_email | |
emailer.prepare_server_conf | |
emailer.send_email |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment