Class: Ratch::Emailer
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.
- #require_smtp ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Emailer
Returns a new instance of Emailer.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ratch/utils/email.rb', line 95 def initialize(={}) require_smtp = .rekey if not [:server] = self.class..merge() end @mailto = [:to] || [:mailto] @from = [:from] @message = [:message] @subject = [:subject] @server = [:server] @account = [:account] @passwd = [:password] @login = [:login] @secure = [:secure] #.to_b @domain = [:domain] @port = [: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 |
Class Attribute Details
.password ⇒ Object
Used for caching password between usages.
58 59 60 |
# File 'lib/ratch/utils/email.rb', line 58 def password @password end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
82 83 84 |
# File 'lib/ratch/utils/email.rb', line 82 def account @account end |
#domain ⇒ Object
Returns the value of attribute domain.
86 87 88 |
# File 'lib/ratch/utils/email.rb', line 86 def domain @domain end |
#from ⇒ Object
Returns the value of attribute from.
87 88 89 |
# File 'lib/ratch/utils/email.rb', line 87 def from @from end |
#login ⇒ Object
Returns the value of attribute login.
84 85 86 |
# File 'lib/ratch/utils/email.rb', line 84 def login @login end |
#mailto ⇒ Object
Returns the value of attribute mailto.
88 89 90 |
# File 'lib/ratch/utils/email.rb', line 88 def mailto @mailto end |
#message ⇒ Object
Returns the value of attribute message.
90 91 92 |
# File 'lib/ratch/utils/email.rb', line 90 def @message end |
#passwd ⇒ Object
Returns the value of attribute passwd.
83 84 85 |
# File 'lib/ratch/utils/email.rb', line 83 def passwd @passwd end |
#port ⇒ Object
Returns the value of attribute port.
81 82 83 |
# File 'lib/ratch/utils/email.rb', line 81 def port @port end |
#secure ⇒ Object
Returns the value of attribute secure.
85 86 87 |
# File 'lib/ratch/utils/email.rb', line 85 def secure @secure end |
#server ⇒ Object
Returns the value of attribute server.
80 81 82 |
# File 'lib/ratch/utils/email.rb', line 80 def server @server end |
#subject ⇒ Object
Returns the value of attribute subject.
89 90 91 |
# File 'lib/ratch/utils/email.rb', line 89 def subject @subject end |
Class Method Details
.environment_options ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ratch/utils/email.rb', line 61 def = {} [:server] = ENV['EMAIL_SERVER'] [:from] = ENV['EMAIL_FROM'] [:account] = ENV['EMAIL_ACCOUNT'] || ENV['EMAIL_FROM'] [:password] = ENV['EMAIL_PASSWORD'] [:port] = ENV['EMAIL_PORT'] [:domain] = ENV['EMAIL_DOMAIN'] [:login] = ENV['EMAIL_LOGIN'] [:secure] = ENV['EMAIL_SECURE'] end |
.new_with_environment(options = {}) ⇒ Object
74 75 76 77 |
# File 'lib/ratch/utils/email.rb', line 74 def new_with_environment(={}) .merge(.rekey) new() end |
Instance Method Details
#email(options = {}) ⇒ Object
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/ratch/utils/email.rb', line 135 def email(={}) .rekey = [:message] || self. subject = [:subject] || self.subject from = [:from] || self.from mailto = [:mailto] || [: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 << #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.(msg, from, mailto) end else Net::SMTP.start(server, port, domain, account, passwd, login) do |smtp| smtp.(msg, from, mailto) end end return mailto rescue Exception => e return e end end |
#password(msg = nil) ⇒ Object
Ask for a password.
FIXME: Does not hide password.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# 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 ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/ratch/utils/email.rb', line 203 def require_smtp begin require 'facets/net/smtp_tls' rescue LoadError require 'net/smtp' end end |