Class: Ratch::Emailer

Inherits:
Object show all
Defined in:
lib/ratch/utils/email.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.



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



58
59
60
# File 'lib/ratch/utils/email.rb', line 58

def password
  @password
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



82
83
84
# File 'lib/ratch/utils/email.rb', line 82

def 
  @account
end

#domainObject

Returns the value of attribute domain.



86
87
88
# File 'lib/ratch/utils/email.rb', line 86

def domain
  @domain
end

#fromObject

Returns the value of attribute from.



87
88
89
# File 'lib/ratch/utils/email.rb', line 87

def from
  @from
end

#loginObject

Returns the value of attribute login.



84
85
86
# File 'lib/ratch/utils/email.rb', line 84

def 
  @login
end

#mailtoObject

Returns the value of attribute mailto.



88
89
90
# File 'lib/ratch/utils/email.rb', line 88

def mailto
  @mailto
end

#messageObject

Returns the value of attribute message.



90
91
92
# File 'lib/ratch/utils/email.rb', line 90

def message
  @message
end

#passwdObject

Returns the value of attribute passwd.



83
84
85
# File 'lib/ratch/utils/email.rb', line 83

def passwd
  @passwd
end

#portObject

Returns the value of attribute port.



81
82
83
# File 'lib/ratch/utils/email.rb', line 81

def port
  @port
end

#secureObject

Returns the value of attribute secure.



85
86
87
# File 'lib/ratch/utils/email.rb', line 85

def secure
  @secure
end

#serverObject

Returns the value of attribute server.



80
81
82
# File 'lib/ratch/utils/email.rb', line 80

def server
  @server
end

#subjectObject

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_optionsObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ratch/utils/email.rb', line 61

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



74
75
76
77
# File 'lib/ratch/utils/email.rb', line 74

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

Instance Method Details

#email(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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(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
    if Net::SMTP.respond_to?(:enable_tls) && secure
      Net::SMTP.enable_tls
      Net::SMTP.start(server, port, domain, , passwd, , secure) do |smtp|
        smtp.send_message(msg, from, mailto)
      end
    else
      Net::SMTP.start(server, port, domain, , passwd, ) do |smtp|
        smtp.send_message(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_smtpObject



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