require 'facets/openhash' module Reap # Emailer class makes it easy send out an email. # # Settings: # # subject Subject of email message. # from Message FROM address [email]. # to Email address to send announcemnt. # server Email server to route message. # port Email server's port. # port_secure Email server's port. # domain Email server's domain name. # account Email account name if needed. # password Password for login.. # login Login type: plain, cram_md5 or login [plain]. # secure Uses TLS security, true or false? [false] # message Mesage to send -or- # file File that contains message. # class Emailer class << self # Used for caching password between usages. attr_accessor :password end attr_accessor :server attr_accessor :port attr_accessor :account attr_accessor :passwd attr_accessor :login attr_accessor :secure attr_accessor :domain attr_accessor :from attr_accessor :mailto attr_accessor :subject attr_accessor :message # def self.load_with_environment(options, defaults={}) options = options.rekey defaults = defaults.rekey environ = environment_options options[:server] ||= environ[:server] options[:from] ||= environ[:from] options[:account] ||= environ[:account] options[:password] ||= environ[:password] options[:port] ||= environ[:port] options[:domain] ||= environ[:domain] options[:login] ||= environ[:login] options[:secure] ||= environ[:secure] options[:server] ||= defaults[:server] options[:from] ||= defaults[:from] options[:account] ||= defaults[:account] || defaults[:from] options[:port] ||= defaults[:port].to_i options[:domain] ||= defaults[:domain] options[:login] ||= defaults[:login] options[:secure] ||= defaults[:secure].to_b new(options) end def self.environment_options options = {} options[:server] ||= ENV['EMAIL_SERVER'] options[:from] ||= ENV['EMAIL_FROM'] options[:account] ||= ENV['EMAIL_ACCOUNT'] || options[:from] options[:password] ||= ENV['EMAIL_PASSWORD'] options[:port] ||= ENV['EMAIL_PORT'].to_i options[:domain] ||= ENV['EMAIL_DOMAIN'] options[:login] ||= ENV['EMAIL_LOGIN'] options[:secure] ||= ENV['EMAIL_SECURE'].to_b options end # def initialize(options) options = options.to_ostruct @server = options.server @account = options.account || options.from @passwd = options.password || self.class.password @login = options.login @secure = options.secure @domain = options.domain || options.server @from = options.from @subject = options.subject @mailto = options.mailto || options.to @message = options.message if options.port @port = options.port else @port = secure ? 465 : 25 end @account ||= @from @login ||= :plain @login = @login.to_sym # save the password for later use self.class.password = @password end # def email(options={}) options.rekey message = options[:message] || message() subject = options[:subject] || subject() from = options[:from] || from() mailto = options[:mailto] || options[:to] || mailto() raise ArgumentError, "missing email field -- server" unless server raise ArgumentError, "missing email field -- account" unless account raise ArgumentError, "missing email field -- from" unless from raise ArgumentError, "missing email field -- mailto" unless mailto raise ArgumentError, "missing email field -- subject" unless subject passwd ||= password("#{account} password:") mailto = [mailto].flatten.compact msg = "" msg << "From: #{from}\n" msg << "To: #{mailto.join(';')}\n" msg << "Subject: #{subject}\n" msg << "" msg << message if secure Net::SMTP.send(:include, Net::SMTP::TLS) Net::SMTP.enable_tls #if secure #if Net::SMTP.respond_to?(:enable_tls) and secure end begin Net::SMTP.start(server, port, domain, account, passwd, login, secure) do |smtp| smtp.send_message(msg, from, mailto) end puts "Email sent successfully to #{mailto.join(';')}." true rescue Exception => e if $DEBUG raise e else abort "Email delivery failed with #{e}" end end end # Ask for a password. # # FIXME: Does not hide password. def password(msg=nil) msg ||= "Enter Password: " inp = '' $stdout << msg inp = STDIN.gets.chomp #begin # system "stty -echo" # inp = gets.chomp #ensure # system "stty echo" #end return inp end end end