Class: Reap::Emailer

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options)
  options = options.to_ostruct

  @server    = options.server
  @account   = options.  || options.from
  @passwd    = options.password || self.class.password
  @login     = options.
  @secure    = options.secure
  @domain    = options.domain || options.server

  @from      = options.from
  @subject   = options.subject
  @mailto    = options.mailto || options.to
  @message   = options.message

  if options.port
    @port = options.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

.passwordObject

Used for caching password between usages.



27
28
29
# File 'lib/reap/emailer.rb', line 27

def password
  @password
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



32
33
34
# File 'lib/reap/emailer.rb', line 32

def 
  @account
end

#domainObject

Returns the value of attribute domain.



36
37
38
# File 'lib/reap/emailer.rb', line 36

def domain
  @domain
end

#fromObject

Returns the value of attribute from.



37
38
39
# File 'lib/reap/emailer.rb', line 37

def from
  @from
end

#loginObject

Returns the value of attribute login.



34
35
36
# File 'lib/reap/emailer.rb', line 34

def 
  @login
end

#mailtoObject

Returns the value of attribute mailto.



38
39
40
# File 'lib/reap/emailer.rb', line 38

def mailto
  @mailto
end

#messageObject

Returns the value of attribute message.



40
41
42
# File 'lib/reap/emailer.rb', line 40

def message
  @message
end

#passwdObject

Returns the value of attribute passwd.



33
34
35
# File 'lib/reap/emailer.rb', line 33

def passwd
  @passwd
end

#portObject

Returns the value of attribute port.



31
32
33
# File 'lib/reap/emailer.rb', line 31

def port
  @port
end

#secureObject

Returns the value of attribute secure.



35
36
37
# File 'lib/reap/emailer.rb', line 35

def secure
  @secure
end

#serverObject

Returns the value of attribute server.



30
31
32
# File 'lib/reap/emailer.rb', line 30

def server
  @server
end

#subjectObject

Returns the value of attribute subject.



39
40
41
# File 'lib/reap/emailer.rb', line 39

def subject
  @subject
end

Class Method Details

.environment_optionsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/reap/emailer.rb', line 69

def self.environment_options
  options = {}

  options[:server]   ||= ENV['EMAIL_SERVER']
  options[:from]     ||= ENV['EMAIL_FROM']
  options[:account]  ||= ENV['EMAIL_ACCOUNT'] || options[:from]
  options[:password] ||= ENV['EMAIL_PASSWORD']
  options[:port]     ||= ENV['EMAIL_PORT'].to_i
  options[:domain]   ||= ENV['EMAIL_DOMAIN']
  options[:login]    ||= ENV['EMAIL_LOGIN']
  options[:secure]   ||= ENV['EMAIL_SECURE'].to_b

  options
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(options, defaults={})
  options  = options.rekey
  defaults = defaults.rekey
  environ  = environment_options

  options[:server]   ||= environ[:server]
  options[:from]     ||= environ[:from]
  options[:account]  ||= environ[:account]
  options[:password] ||= environ[:password]
  options[:port]     ||= environ[:port]
  options[:domain]   ||= environ[:domain]
  options[:login]    ||= environ[:login]
  options[:secure]   ||= environ[:secure]

  options[:server]   ||= defaults[:server]
  options[:from]     ||= defaults[:from]
  options[:account]  ||= defaults[:account] || defaults[:from]
  options[:port]     ||= defaults[:port].to_i
  options[:domain]   ||= defaults[:domain]
  options[:login]    ||= defaults[:login]
  options[:secure]   ||= defaults[:secure].to_b

  new(options)
end

Instance Method Details

#email(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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(options={})
  options.rekey

  message = options[:message] || message()
  subject = options[:subject] || subject()
  from    = options[:from]    || from()
  mailto  = options[:mailto]  || options[:to] || 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

  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, , passwd, , secure) do |smtp|
      smtp.send_message(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