Class: Eco::API::Common::Session::Mailer

Inherits:
Object
  • Object
show all
Includes:
Language::AuxiliarLogger
Defined in:
lib/eco/api/common/session/mailer.rb,
lib/eco/api/common/session/mailer/aws_provider.rb,
lib/eco/api/common/session/mailer/provider_base.rb,
lib/eco/api/common/session/mailer/sendgrid_provider.rb

Defined Under Namespace

Classes: AwsProvider, ProviderBase, SendgridProvider

Constant Summary collapse

DEFAULT_PROVIDER =
:sendgrid

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Constructor Details

#initialize(enviro:, provider: DEFAULT_PROVIDER) ⇒ Mailer

Returns a new instance of Mailer.



18
19
20
21
22
23
24
# File 'lib/eco/api/common/session/mailer.rb', line 18

def initialize(enviro:, provider: DEFAULT_PROVIDER)
  msg = "Required Environment object (enviro:). Given: #{enviro.class}"
  raise msg if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment)

  @enviro   = enviro
  @provider = provider || DEFAULT_PROVIDER
end

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



15
16
17
# File 'lib/eco/api/common/session/mailer.rb', line 15

def provider
  @provider
end

Instance Method Details

#mail(subject:, body:, to: nil, cc: nil, bcc: nil) ⇒ Object

Sends an email

Parameters:

  • to (String) (defaults to: nil)

    destination email address

  • subject (String)

    subject of the email

  • body (String)

    html or plain text message



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/eco/api/common/session/mailer.rb', line 30

def mail(subject:, body:, to: nil, cc: nil, bcc: nil)
  return false unless (serv = service)

  unless serv.configured?
    msg  = "Mailer: You are missing configuration parameters "
    msg << "for '#{provider}'. Review your .env file"
    log(:error) { msg }
    return false
  end

  serv.send_mail(
    subject: subject,
    body:    body,
    to:      to,
    cc:      cc,
    bcc:     bcc
  ).tap do |response|
    next unless response

    to_addr = serv.fetch_to(to)
    # msg = "Sent email (MessageId: #{response.message_id}) to #{fetch_destination(to: to, cc: cc, bcc: bcc)}"
    msg = "Sent email #{ProviderBase.to_desc(to: to_addr, cc: cc, bcc: bcc)}"
    puts msg
    log(:debug) { msg }
  end
end