class Ratch::Emailer
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.
Attributes
password[RW]
Used for caching password between usages.
account[RW]
domain[RW]
from[RW]
login[RW]
mailto[RW]
message[RW]
passwd[RW]
port[RW]
secure[RW]
server[RW]
subject[RW]
Public Class Methods
environment_options()
click to toggle source
# File lib/ratch/utils/email.rb, line 61 def environment_options options = {} options[:server] = ENV['EMAIL_SERVER'] options[:from] = ENV['EMAIL_FROM'] options[:account] = ENV['EMAIL_ACCOUNT'] || ENV['EMAIL_FROM'] options[:password] = ENV['EMAIL_PASSWORD'] options[:port] = ENV['EMAIL_PORT'] options[:domain] = ENV['EMAIL_DOMAIN'] options[:login] = ENV['EMAIL_LOGIN'] options[:secure] = ENV['EMAIL_SECURE'] options end
new(options={})
click to toggle source
# File lib/ratch/utils/email.rb, line 95 def initialize(options={}) require_smtp options = options.rekey if not options[:server] options = self.class.environment_options.merge(options) end @mailto = options[:to] || options[:mailto] @from = options[:from] @message = options[:message] @subject = options[:subject] @server = options[:server] @account = options[:account] @passwd = options[:password] @login = options[:login] @secure = options[:secure] #.to_b @domain = options[:domain] @port = options[:port] @port ||= secure ? 465 : 25 @port = @port.to_i @account ||= @from @login ||= :plain @login = @login.to_sym @passwd ||= self.class.password @domain ||= @server # save the password for later use self.class.password = @passwd end
new_with_environment(options={})
click to toggle source
# File lib/ratch/utils/email.rb, line 74 def new_with_environment(options={}) environment_options.merge(options.rekey) new(options) end
Public Instance Methods
email(options={})
click to toggle source
# File lib/ratch/utils/email.rb, line 135 def email(options={}) options.rekey message = options[:message] || self.message subject = options[:subject] || self.subject from = options[:from] || self.from mailto = options[:mailto] || options[:to] || self.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 #p server, port, domain, account, passwd, login, secure if verbose? begin if Net::SMTP.respond_to?(:enable_tls) && secure Net::SMTP.enable_tls Net::SMTP.start(server, port, domain, account, passwd, login, secure) do |smtp| smtp.send_message(msg, from, mailto) end else Net::SMTP.start(server, port, domain, account, passwd, login) do |smtp| smtp.send_message(msg, from, mailto) end end return mailto rescue Exception => e return e end end
password(msg=nil)
click to toggle source
Ask for a password.
FIXME: Does not hide password.
# File lib/ratch/utils/email.rb, line 184 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
require_smtp()
click to toggle source
# File lib/ratch/utils/email.rb, line 203 def require_smtp begin require 'facets/net/smtp_tls' rescue LoadError require 'net/smtp' end end