Class: Reap::Emailer
- Inherits:
-
Object
- Object
- Reap::Emailer
- Defined in:
- lib/reap/emailer.rb
Overview
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 Attribute Summary collapse
-
.password ⇒ Object
Used for caching password between usages.
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#from ⇒ Object
Returns the value of attribute from.
-
#login ⇒ Object
Returns the value of attribute login.
-
#mailto ⇒ Object
Returns the value of attribute mailto.
-
#message ⇒ Object
Returns the value of attribute message.
-
#passwd ⇒ Object
Returns the value of attribute passwd.
-
#port ⇒ Object
Returns the value of attribute port.
-
#secure ⇒ Object
Returns the value of attribute secure.
-
#server ⇒ Object
Returns the value of attribute server.
-
#subject ⇒ Object
Returns the value of attribute subject.
Class Method Summary collapse
Instance Method Summary collapse
- #email(options = {}) ⇒ Object
-
#initialize(options) ⇒ Emailer
constructor
A new instance of Emailer.
-
#password(msg = nil) ⇒ Object
Ask for a password.
Constructor Details
#initialize(options) ⇒ Emailer
Returns a new instance of Emailer.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/reap/emailer.rb', line 86 def initialize() = .to_ostruct @server = .server @account = .account || .from @passwd = .password || self.class.password @login = .login @secure = .secure @domain = .domain || .server @from = .from @subject = .subject @mailto = .mailto || .to @message = . if .port @port = .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 |
Class Attribute Details
.password ⇒ Object
Used for caching password between usages.
27 28 29 |
# File 'lib/reap/emailer.rb', line 27 def password @password end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
32 33 34 |
# File 'lib/reap/emailer.rb', line 32 def account @account end |
#domain ⇒ Object
Returns the value of attribute domain.
36 37 38 |
# File 'lib/reap/emailer.rb', line 36 def domain @domain end |
#from ⇒ Object
Returns the value of attribute from.
37 38 39 |
# File 'lib/reap/emailer.rb', line 37 def from @from end |
#login ⇒ Object
Returns the value of attribute login.
34 35 36 |
# File 'lib/reap/emailer.rb', line 34 def login @login end |
#mailto ⇒ Object
Returns the value of attribute mailto.
38 39 40 |
# File 'lib/reap/emailer.rb', line 38 def mailto @mailto end |
#message ⇒ Object
Returns the value of attribute message.
40 41 42 |
# File 'lib/reap/emailer.rb', line 40 def @message end |
#passwd ⇒ Object
Returns the value of attribute passwd.
33 34 35 |
# File 'lib/reap/emailer.rb', line 33 def passwd @passwd end |
#port ⇒ Object
Returns the value of attribute port.
31 32 33 |
# File 'lib/reap/emailer.rb', line 31 def port @port end |
#secure ⇒ Object
Returns the value of attribute secure.
35 36 37 |
# File 'lib/reap/emailer.rb', line 35 def secure @secure end |
#server ⇒ Object
Returns the value of attribute server.
30 31 32 |
# File 'lib/reap/emailer.rb', line 30 def server @server end |
#subject ⇒ Object
Returns the value of attribute subject.
39 40 41 |
# File 'lib/reap/emailer.rb', line 39 def subject @subject end |
Class Method Details
.environment_options ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/reap/emailer.rb', line 69 def self. = {} [:server] ||= ENV['EMAIL_SERVER'] [:from] ||= ENV['EMAIL_FROM'] [:account] ||= ENV['EMAIL_ACCOUNT'] || [:from] [:password] ||= ENV['EMAIL_PASSWORD'] [:port] ||= ENV['EMAIL_PORT'].to_i [:domain] ||= ENV['EMAIL_DOMAIN'] [:login] ||= ENV['EMAIL_LOGIN'] [:secure] ||= ENV['EMAIL_SECURE'].to_b end |
.load_with_environment(options, defaults = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/reap/emailer.rb', line 44 def self.load_with_environment(, defaults={}) = .rekey defaults = defaults.rekey environ = [:server] ||= environ[:server] [:from] ||= environ[:from] [:account] ||= environ[:account] [:password] ||= environ[:password] [:port] ||= environ[:port] [:domain] ||= environ[:domain] [:login] ||= environ[:login] [:secure] ||= environ[:secure] [:server] ||= defaults[:server] [:from] ||= defaults[:from] [:account] ||= defaults[:account] || defaults[:from] [:port] ||= defaults[:port].to_i [:domain] ||= defaults[:domain] [:login] ||= defaults[:login] [:secure] ||= defaults[:secure].to_b new() end |
Instance Method Details
#email(options = {}) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/reap/emailer.rb', line 118 def email(={}) .rekey = [:message] || () subject = [:subject] || subject() from = [:from] || from() mailto = [:mailto] || [: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 << 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.(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 |
#password(msg = nil) ⇒ Object
Ask for a password.
FIXME: Does not hide password.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/reap/emailer.rb', line 168 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 |