Class: Obscured::Doorman::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/obscured-doorman/mailer.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Mailer

Returns a new instance of Mailer.



6
7
8
9
10
11
12
13
# File 'lib/obscured-doorman/mailer.rb', line 6

def initialize(opts = {})
  @to = opts[:to]
  @from = "doorman@#{Doorman.configuration.smtp_domain}"
  @subject = opts[:subject]

  @text = opts[:text]
  @html = opts[:html]
end

Instance Method Details

#deliver!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/obscured-doorman/mailer.rb', line 15

def deliver!
  Doorman.logger.debug "Sending mail to #{@to}, from: #{@from}, with subject: #{@subject} and text #{@text}"
  mail = Mail.new(to: @to, from: @from, subject: @subject) do
    delivery_method :smtp,
                    address: Doorman.configuration.smtp_server,
                    port: Doorman.configuration.smtp_port,
                    domain: Doorman.configuration.smtp_domain,
                    enable_starttls_auto: true,
                    authentication: :plain,
                    user_name: Doorman.configuration.smtp_username,
                    password: Doorman.configuration.smtp_password
  end

  unless @text.blank?
    text_part = Mail::Part.new(body: @text)
    mail.text_part = text_part
  end

  unless @html.blank?
    html_part = Mail::Part.new(body: @html) do
      content_type 'text/html; charset=utf-8'
    end
    mail.html_part = html_part
  end

  mail.deliver
rescue => e
  Doorman.logger.error e
end