Class: Envoy::Email

Inherits:
Transport show all
Defined in:
lib/envoy/transport.rb

Instance Attribute Summary collapse

Attributes inherited from Transport

#errors

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Email

Returns a new instance of Email.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/envoy/transport.rb', line 95

def initialize(options = {})
  self.host = options[:host]
  self.username = options[:username]
  self.sender = options[:sender]
  self.password = options[:password]
  self.to = options[:to]
  self.port = options[:port] || 25
  self.ssl = options[:ssl] || false
  self.authentication = options[:authentication] || nil
  self.domain = options[:domain]
  self.openssl_verify_mode = options[:openssl_verify_mode]
end

Instance Attribute Details

#authenticationObject

Returns the value of attribute authentication.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def authentication
  @authentication
end

#domainObject

Returns the value of attribute domain.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def domain
  @domain
end

#hostObject

Returns the value of attribute host.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def host
  @host
end

#openssl_verify_modeObject

Returns the value of attribute openssl_verify_mode.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def openssl_verify_mode
  @openssl_verify_mode
end

#passwordObject

Returns the value of attribute password.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def password
  @password
end

#portObject

Returns the value of attribute port.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def port
  @port
end

#senderObject

Returns the value of attribute sender.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def sender
  @sender
end

#sslObject

Returns the value of attribute ssl.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def ssl
  @ssl
end

#toObject

Returns the value of attribute to.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def to
  @to
end

#usernameObject

Returns the value of attribute username.



93
94
95
# File 'lib/envoy/transport.rb', line 93

def username
  @username
end

Instance Method Details

#build_connectionObject



124
125
126
127
128
129
130
131
132
# File 'lib/envoy/transport.rb', line 124

def build_connection
  smtp = Net::SMTP.new(@host, @port)

  if using_authentication?
    @openssl_verify_mode.nil? ? smtp.send(tls_mode) : smtp.send(tls_mode, @openss_verify_mode)
  end

  return smtp
end

#build_mail(message) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/envoy/transport.rb', line 113

def build_mail(message)
  mail = TMail::Mail.new
  mail.to = @to.is_a?(String) ? @to : @to.join(',')
  mail.from = @sender.nil? ? 'Envoy Messenger <envoymessenger@localhost>' : @sender
  mail.subject = message.subject
  mail.date = Time.now
  mail.body = message.body || message.subject

  mail
end

#send_message(message) ⇒ Object



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
# File 'lib/envoy/transport.rb', line 142

def send_message(message)
  mail = self.build_mail(message)

  if @host.to_sym == :sendmail
    IO.popen("#{self.sendmail_binary} -f #{mail.from}, #{mail.to}", "w+") do |io|
      io.puts mail
      io.flush
    end
  else
    smtp = build_connection
    if using_authentication?
      smtp.start(@domain, @username, @password, @authentication.to_sym) do |smtp|
        smtp.sendmail mail.to_s, mail.from, mail.to
      end
    else
      smtp.start do |smtp|
        smtp.sendmail mail.to_s, mail.from, mail.to
      end
    end
  end

  return true

  rescue StandardError => error
    self.errors << SendError.new(error, Time.now)
    return false
end

#sendmail_binaryObject



108
109
110
111
# File 'lib/envoy/transport.rb', line 108

def sendmail_binary
  sendmail = `which sendmail`.chomp
  (sendmail.nil? || sendmail == '') ? '/usr/bin/sendmail' : sendmail
end

#tls_modeObject



134
135
136
# File 'lib/envoy/transport.rb', line 134

def tls_mode
  RUBY_VERSION <= '1.8.6' ? "enable_tls" : "enable_starttls_auto"
end

#using_authentication?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/envoy/transport.rb', line 138

def using_authentication?
  !@username.nil? && !@password.nil? && !@authentication.nil? && @ssl
end