Class: Detroit::Emailer

Inherits:
Object show all
Defined in:
lib/detroit/email_utils.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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(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

Class Attribute Details

.passwordObject

Used for caching password between usages.



60
61
62
# File 'lib/detroit/email_utils.rb', line 60

def password
  @password
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



84
85
86
# File 'lib/detroit/email_utils.rb', line 84

def 
  @account
end

#domainObject

Returns the value of attribute domain.



88
89
90
# File 'lib/detroit/email_utils.rb', line 88

def domain
  @domain
end

#fromObject

Returns the value of attribute from.



89
90
91
# File 'lib/detroit/email_utils.rb', line 89

def from
  @from
end

#loginObject

Returns the value of attribute login.



86
87
88
# File 'lib/detroit/email_utils.rb', line 86

def 
  @login
end

#mailtoObject

Returns the value of attribute mailto.



90
91
92
# File 'lib/detroit/email_utils.rb', line 90

def mailto
  @mailto
end

#messageObject

Returns the value of attribute message.



92
93
94
# File 'lib/detroit/email_utils.rb', line 92

def message
  @message
end

#passwdObject

Returns the value of attribute passwd.



85
86
87
# File 'lib/detroit/email_utils.rb', line 85

def passwd
  @passwd
end

#portObject

Returns the value of attribute port.



83
84
85
# File 'lib/detroit/email_utils.rb', line 83

def port
  @port
end

#secureObject

Returns the value of attribute secure.



87
88
89
# File 'lib/detroit/email_utils.rb', line 87

def secure
  @secure
end

#serverObject

Returns the value of attribute server.



82
83
84
# File 'lib/detroit/email_utils.rb', line 82

def server
  @server
end

#subjectObject

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_optionsObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/detroit/email_utils.rb', line 63

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_with_environment(options = {}) ⇒ Object



76
77
78
79
# File 'lib/detroit/email_utils.rb', line 76

def new_with_environment(options={})
  environment_options.merge(options.rekey)
  new(options)
end

Instance Method Details

#email(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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(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 

  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("#{} 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
    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, , passwd, ) do |smtp|
      smtp.send_message(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_smtpObject



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