Class: Detroit::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.
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 132 133 |
# File 'lib/detroit/email_utils.rb', line 97 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.
60 61 62 |
# File 'lib/detroit/email_utils.rb', line 60 def password @password end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
84 85 86 |
# File 'lib/detroit/email_utils.rb', line 84 def account @account end |
#domain ⇒ Object
Returns the value of attribute domain.
88 89 90 |
# File 'lib/detroit/email_utils.rb', line 88 def domain @domain end |
#from ⇒ Object
Returns the value of attribute from.
89 90 91 |
# File 'lib/detroit/email_utils.rb', line 89 def from @from end |
#login ⇒ Object
Returns the value of attribute login.
86 87 88 |
# File 'lib/detroit/email_utils.rb', line 86 def login @login end |
#mailto ⇒ Object
Returns the value of attribute mailto.
90 91 92 |
# File 'lib/detroit/email_utils.rb', line 90 def mailto @mailto end |
#message ⇒ Object
Returns the value of attribute message.
92 93 94 |
# File 'lib/detroit/email_utils.rb', line 92 def @message end |
#passwd ⇒ Object
Returns the value of attribute passwd.
85 86 87 |
# File 'lib/detroit/email_utils.rb', line 85 def passwd @passwd end |
#port ⇒ Object
Returns the value of attribute port.
83 84 85 |
# File 'lib/detroit/email_utils.rb', line 83 def port @port end |
#secure ⇒ Object
Returns the value of attribute secure.
87 88 89 |
# File 'lib/detroit/email_utils.rb', line 87 def secure @secure end |
#server ⇒ Object
Returns the value of attribute server.
82 83 84 |
# File 'lib/detroit/email_utils.rb', line 82 def server @server end |
#subject ⇒ Object
Returns the value of attribute subject.
91 92 93 |
# File 'lib/detroit/email_utils.rb', line 91 def subject @subject end |
Class Method Details
.environment_options ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/detroit/email_utils.rb', line 63 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
76 77 78 79 |
# File 'lib/detroit/email_utils.rb', line 76 def new_with_environment(={}) .merge(.rekey) new() end |
Instance Method Details
#email(options = {}) ⇒ Object
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 179 180 181 182 |
# File 'lib/detroit/email_utils.rb', line 137 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 smtp = Net::SMTP.new(server, port) if smtp.respond_to?(:enable_starttls_auto) smtp.enable_starttls_auto elsif secure #&& smtp.respond_to?(:enable_tls) smtp.enable_tls end smtp.start(domain, account, passwd, login) do |smtp| smtp.(msg, from, mailto) end return mailto rescue Exception => e return e end end |
#password(msg = nil) ⇒ Object
Ask for a password.
FIXME: Does not hide password.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/detroit/email_utils.rb', line 188 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
207 208 209 210 211 212 213 |
# File 'lib/detroit/email_utils.rb', line 207 def require_smtp begin require 'net/smtp_tls' rescue LoadError require 'net/smtp' end end |