Ratch  Ratch::Emailer

[Validate]
Generated with Newfish 0.1.0

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.

server[RW]
port[RW]
account[RW]
passwd[RW]
login[RW]
secure[RW]
domain[RW]
from[RW]
mailto[RW]
subject[RW]
message[RW]

Public Class Methods

environment_options() click to toggle source
    # File lib/ratch/utils/email.rb, line 61
61:       def environment_options
62:         options = {}
63:         options[:server]   = ENV['EMAIL_SERVER']
64:         options[:from]     = ENV['EMAIL_FROM']
65:         options[:account]  = ENV['EMAIL_ACCOUNT'] || ENV['EMAIL_FROM']
66:         options[:password] = ENV['EMAIL_PASSWORD']
67:         options[:port]     = ENV['EMAIL_PORT']
68:         options[:domain]   = ENV['EMAIL_DOMAIN']
69:         options[:login]    = ENV['EMAIL_LOGIN']
70:         options[:secure]   = ENV['EMAIL_SECURE']
71:         options
72:       end
new(options={}) click to toggle source
     # File lib/ratch/utils/email.rb, line 95
 95:     def initialize(options={})
 96:       require_smtp
 97: 
 98:       options = options.rekey
 99: 
100:       if not options[:server]
101:         options = self.class.environment_options.merge(options)
102:       end
103: 
104:       @mailto    = options[:to] || options[:mailto]
105: 
106:       @from      = options[:from]
107:       @message   = options[:message]
108:       @subject   = options[:subject]
109:       @server    = options[:server]
110:       @account   = options[:account]
111:       @passwd    = options[:password]
112:       @login     = options[:login]
113:       @secure    = options[:secure] #.to_b
114:       @domain    = options[:domain]
115:       @port      = options[:port]
116: 
117:       @port    ||= secure ? 465 : 25
118:       @port = @port.to_i
119: 
120:       @account ||= @from
121: 
122:       @login   ||= :plain
123:       @login = @login.to_sym
124: 
125:       @passwd ||= self.class.password
126: 
127:       @domain ||= @server
128: 
129:       # save the password for later use
130:       self.class.password = @passwd
131:     end
new_with_environment(options={}) click to toggle source
    # File lib/ratch/utils/email.rb, line 74
74:       def new_with_environment(options={})
75:         environment_options.merge(options.rekey)
76:         new(options)
77:       end

Public Instance Methods

email(options={}) click to toggle source
     # File lib/ratch/utils/email.rb, line 135
135:     def email(options={})
136:       options.rekey
137: 
138:       message = options[:message] || self.message
139:       subject = options[:subject] || self.subject
140:       from    = options[:from]    || self.from
141:       mailto  = options[:mailto]  || options[:to] || self.mailto
142: 
143:       raise ArgumentError, "missing email field -- server"  unless server
144:       raise ArgumentError, "missing email field -- account" unless account
145: 
146:       raise ArgumentError, "missing email field -- from"    unless from
147:       raise ArgumentError, "missing email field -- mailto"  unless mailto
148:       raise ArgumentError, "missing email field -- subject" unless subject
149: 
150:       passwd ||= password("#{account} password:")
151: 
152:       mailto = [mailto].flatten.compact
153: 
154:       msg = ""
155:       msg << "From: #{from}\n"
156:       msg << "To: #{mailto.join(';')}\n"
157:       msg << "Subject: #{subject}\n"
158:       msg << ""
159:       msg << message
160: 
161:       #p server, port, domain, account, passwd, login, secure if verbose?
162: 
163:       begin
164:         if Net::SMTP.respond_to?(:enable_tls) && secure
165:           Net::SMTP.enable_tls
166:           Net::SMTP.start(server, port, domain, account, passwd, login, secure) do |smtp|
167:             smtp.send_message(msg, from, mailto)
168:           end
169:         else
170:           Net::SMTP.start(server, port, domain, account, passwd, login) do |smtp|
171:             smtp.send_message(msg, from, mailto)
172:           end
173:         end
174:         return mailto
175:       rescue Exception => e
176:         return e
177:       end
178:     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
184:     def password(msg=nil)
185:       msg ||= "Enter Password: "
186:       inp = ''
187: 
188:       $stdout << msg
189: 
190:       inp = STDIN.gets.chomp
191: 
192:       #begin
193:       #  system "stty -echo"
194:       #  inp = gets.chomp
195:       #ensure
196:       #  system "stty echo"
197:       #end
198: 
199:       return inp
200:     end
require_smtp() click to toggle source
     # File lib/ratch/utils/email.rb, line 203
203:     def require_smtp
204:       begin
205:         require 'facets/net/smtp_tls'
206:       rescue LoadError
207:         require 'net/smtp'
208:       end
209:     end

Disabled; run with --debug to generate this.