Class: EcoRake::Utils::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/eco-rake/utils/mailer.rb,
lib/eco-rake/utils/mailer/aws_provider.rb,
lib/eco-rake/utils/mailer/provider_base.rb,
lib/eco-rake/utils/mailer/sendgrid_provider.rb

Overview

Mailer helper that uses aws-sdk-ses gem (Aws::SES::Client) Several ENV vars should be configured for it to work (i.e. in the .env file):

  1. MAILER_REGION
  2. MAILER_KEY_ID
  3. MAILER_ACCESS_KEY
  4. MAILER_FROM

Defined Under Namespace

Classes: AwsProvider, ProviderBase, SendgridProvider

Constant Summary collapse

DEFAULT_PROVIDER =
:sendgrid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider: DEFAULT_PROVIDER) ⇒ Mailer

Returns a new instance of Mailer.



18
19
20
# File 'lib/eco-rake/utils/mailer.rb', line 18

def initialize(provider: DEFAULT_PROVIDER)
  @provider = provider || DEFAULT_PROVIDER
end

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



16
17
18
# File 'lib/eco-rake/utils/mailer.rb', line 16

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/eco-rake/utils/mailer.rb', line 26

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"
    puts 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 #{ProviderBase.to_desc(to: to_addr, cc: cc, bcc: bcc)}"
    puts msg
  end
rescue StandardError => err
  msg  = "The mailer generated an exception:\n"
  msg << err.patch_full_message(trace_count: 15)
  puts msg
end